In this section, we will learn about the drop-down property settings of user controls. You can set properties for user controls to be displayed in a drop-down box, as shown below:
Here we define a user control and set a property DropDownPropery
The Code is as follows:
[Csharp]
Public partial class UCLab: UserControl
{
Public UCLab ()
{
InitializeComponent ();
}
Private string dropdownPropery = "Hello ";
[Description ("drop-down property")]
[Editor (typeof (DropTypeDialogEditor), typeof (UITypeEditor)]
Public string DropDownPropery
{
Get
{
Return dropdownPropery;
}
Set
{
DropdownPropery = value;
}
}
}
Public partial class UCLab: UserControl
{
Public UCLab ()
{
InitializeComponent ();
}
Private string dropdownPropery = "Hello ";
[Description ("drop-down property")]
[Editor (typeof (DropTypeDialogEditor), typeof (UITypeEditor)]
Public string DropDownPropery
{
Get
{
Return dropdownPropery;
}
Set
{
DropdownPropery = value;
}
}
}
Next, set a drop-down editor for this attribute. The Code is as follows:
[Csharp]
/// <Summary>
/// Drop-down Editor
/// </Summary>
Public class DropTypeDialogEditor: UITypeEditor
{
Public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
{
If (context! = Null & context. Instance! = Null)
{
Return UITypeEditorEditStyle. DropDown; // display the drop-down arrow
}
Return base. GetEditStyle (context );
}
Public override object EditValue (ITypeDescriptorContext context, IServiceProvider provider, object value)
{
System. Windows. Forms. Design. IWindowsFormsEditorService editorService = null;
If (context! = Null & context. Instance! = Null & provider! = Null)
{
EditorService = (System. Windows. Forms. Design. IWindowsFormsEditorService) provider. GetService (typeof (System. Windows. Forms. Design. IWindowsFormsEditorService ));
If (editorService! = Null)
{
UCLab lab = (UCLab) context. Instance;
Ucddrop = new ucddrop (lab. DropDownPropery );
EditorService. DropDownControl (drop );
Value = drop. Result;
Return value;
}
}
// Return base. EditValue (context, provider, value );
Return value;
}
}
/// <Summary>
/// Drop-down Editor
/// </Summary>
Public class DropTypeDialogEditor: UITypeEditor
{
Public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
{
If (context! = Null & context. Instance! = Null)
{
Return UITypeEditorEditStyle. DropDown; // display the drop-down arrow
}
Return base. GetEditStyle (context );
}
Public override object EditValue (ITypeDescriptorContext context, IServiceProvider provider, object value)
{
System. Windows. Forms. Design. IWindowsFormsEditorService editorService = null;
If (context! = Null & context. Instance! = Null & provider! = Null)
{
EditorService = (System. Windows. Forms. Design. IWindowsFormsEditorService) provider. GetService (typeof (System. Windows. Forms. Design. IWindowsFormsEditorService ));
If (editorService! = Null)
{
UCLab lab = (UCLab) context. Instance;
Ucddrop = new ucddrop (lab. DropDownPropery );
EditorService. DropDownControl (drop );
Value = drop. Result;
Return value;
}
}
// Return base. EditValue (context, provider, value );
Return value;
}
}
The above code can be implemented. When we drag the user control to the form and set this attribute, We can display the following pull form.