传统 ActiveX (Forms 2.0) 控件类型

传统 ActiveX (Forms 2.0) 控件类型

旧版 ActiveX(Forms 2.0)控件类型

使用 OLE 对象和嵌入内容 涵盖如何通过嵌入的 Microsoft Forms 2.0 ActiveX 控件进行访问 OleFormat.OleControl 并将其转换为抽象 Forms2OleControl 基类。 Aspose.Words 的 FOSS 用于 .NET 还公开了 Word 支持的五种特定控件类型的强类型子类—— CheckBoxControl, OptionButtonControl, TextBoxControl, CommandButtonControl,以及 MorphDataControl —— 每个都在的 Aspose.Words.Drawing.Ole 命名空间,在属性之上添加一个特定于控件的成员 Forms2OleControl 已经提供(Caption, Value, Enabled, Type, GroupName, ForeColor, BackColor, Width, Height, Name, ChildNodes).


CheckBoxControl 和 OptionButtonControl

CheckBoxControl 扩展自 MorphDataControl 并添加 Checked(一个 bool),用于控件的开/关状态。OptionButtonControl 也扩展自 MorphDataControl 并添加 Selected(一个 bool)——用于一组共享相同 GroupName 的互斥选项按钮。


TextBoxControl

TextBoxControl 扩展自 MorphDataControl 并添加 Text(一个 string),即控件显示和可编辑的文本。


CommandButtonControl

CommandButtonControl 直接继承自 Forms2OleControl(而非 MorphDataControl),并拥有自己的构造函数。它表示在 Word 中点击时运行宏的按钮,其按钮标签通过继承的 Caption 属性公开。


MorphDataControl:数据录入控件的共享基类

MorphDataControl 是六种相关控件类型的聚合基类——复选框、组合框、列表框、选项按钮、文本框和切换按钮。CheckBoxControl、OptionButtonControl 和 TextBoxControl 各自继承它,获得其成员并各自添加一个特定类型的属性。


提示与最佳实践

  • 在将 Forms2OleControl.Type(一个 Forms2OleControlType 值)转换为特定子类之前读取它以识别控件——并非所有 Forms2OleControl 都是此处涵盖的五个强类型子类之一。
  • 使用 GroupName 将多个 OptionButtonControl 实例关联为互斥集合,以匹配 Word 对选项按钮的分组方式。
  • 通过继承的 ChildNodes(一个 Forms2OleControlCollection)访问帧或多页容器内部的嵌套控件,无论容器属于哪个子类。

常见问题

问题原因修复
将 Forms2OleControl 强制转换为 CheckBoxControl 会抛出 InvalidCastException该控件是不同的 Forms 2.0 控件类型(例如 TextBoxControl 或 OptionButtonControl)在强制转换为特定子类之前检查 Forms2OleControl.Type
OptionButtonControl.Selected 在与兄弟控件一起使用时并未表现为互斥这些控件未共享相同的 GroupName在集合中的每个 OptionButtonControl 上设置相同的 GroupName 值
CommandButtonControl.Caption 为空按钮的标题来源于继承的 Forms2OleControl.Caption,而不是 CommandButtonControl 特有的属性读取 Caption,而不是不存在的仅 CommandButtonControl 成员

FAQ

我如何知道 Forms2OleControl 实际属于哪种强类型控件类?

读取其 Type 属性(Forms2OleControlType),或尝试将其强制转换为您期望的具体子类,例如 CheckBoxControl 或 TextBoxControl。

MorphDataControl 与 Forms2OleControl 有何区别?

Forms2OleControl 是所有 Forms 2.0 控件的抽象基类。MorphDataControl 是更具体的抽象基类,继承自 Forms2OleControl,用于复选框、组合框、列表框、选项按钮、文本框和切换按钮等控件类型。

我如何读取复选框控件的选中状态?

将 Forms2OleControl 强制转换为 CheckBoxControl,并读取其 Checked 属性。

CommandButtonControl 是否像其他控件一样扩展自 MorphDataControl?

不 – CommandButtonControl 直接扩展 Forms2OleControl,因为命令按钮不是数据输入控件。


API Reference 摘要

类描述
MorphDataControl复选框、组合框、列表框、选项按钮、文本框和切换按钮控件的抽象基类
CheckBoxControl复选框控件; 扩展 MorphDataControl; 添加 Checked
OptionButtonControl选项按钮控件; 扩展 MorphDataControl; 添加 Selected
TextBoxControl文本框控件; 扩展 MorphDataControl; 添加 Text
CommandButtonControl命令按钮控件; 直接扩展 Forms2OleControl

另请参阅

 中文