一个标志编辑器






4.73/5 (32投票s)
2002年10月18日

153178

747
这段代码实现了简单的标志编辑器。它可以用来在属性网格中编辑标志。
引言
此编辑器显示一个下拉控件,其中包含一个带有枚举所有值的 CheckedListBox
。
使用 FlagsEditor
你只需要将 Editor
属性放在一个枚举上,就可以将其与 FlagsEditor
关联起来。 还可以为每个值添加一个 Description
属性。 它将显示为 CheckedListBox
项目上的工具提示。
这是一个示例。
[Flags,
Editor(typeof(STUP.ComponentModel.Design.FlagsEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public enum EnumerationTest
{
[Description("Description for the first tested value.")]
firstValue = 1,
[Description("Description for the second tested value.")]
secondValue = 2,
[Description("Description for the third tested value.")]
thirdValue = 4
}
它是如何工作的?
Editor
只是一个继承自 UITypeEditor
的类。 Editor
的行为由两个函数控制。
GetEditStyle
此函数用于控制属性网格中小型按钮的外观。 在此示例中,将显示一个下拉箭头。
public override UITypeEditorEditStyle
GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
EditValue
当用户单击小型按钮时,将调用此函数。
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
if (context != null
&& context.Instance != null
&& provider != null)
{
// Get an instance of the IWindowsFormsEditorService.
edSvc = (IWindowsFormsEditorService)provider.GetService
(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
// Create a CheckedListBox
clb = new CheckedListBox();
...
// Show our CheckedListbox as a DropDownControl.
// This methods returns only when
// the dropdowncontrol is closed
edSvc.DropDownControl(clb);
// return the right enum value
// corresponding to the result
return ...
}
}
return value;
}
可以通过调用 edSvc.CloseDropDown()
函数来关闭 DropDownControl
。
有关完整示例,请下载源代码文件。