. Net WinForm user control development -- (7) User Control drop-down property settings

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.