To achieve this, we only need to do two things.
1. In the control class library,Add a windowform class and edit the corresponding interfaceAnd initialize
Because the control is definedPrivateSo we change itPublic.
Public partial class categorywindow: Form
{
Public categorywindow ()
{
Initializecomponent ();
Setselectdata ();
}
Public void setselectdata ()
{
Try
{
This. combobox1.items. Add ("Fruit ");
This. combobox1.items. Add ("Vegetables ");
This. combobox1.items. Add ("meat ");
This. combobox1.items. Add ("eggs ");
This. combobox1.items. Add ("Pasta ");
This. combobox1.selecteditem = combobox1.items [0];
}
Catch (exception EEE)
{
Throw EEE;
}
Finally
{
}
}
}
2. Customize your own uitypeeditor class.
Public class categorymodaleditor: system. Drawing. Design. uitypeeditor
{
Public categorymodaleditor ()
{
}
Public override system. Drawing. Design. uitypeeditoreditstyle geteditstyle (system. componentmodel. itypedescriptorcontext context)
{
Return uitypeeditoreditstyle. Modal;
}
Public override object editvalue (system. componentmodel. itypedescriptorcontext context,
System. iserviceprovider provider, object value)
{
Iwindowsformseditorservice service =
(Iwindowsformseditorservice) provider. getservice (typeof (iwindowsformseditorservice ));
If (service = NULL)
{
Return NULL;
}
Categorywindow form = new categorywindow ();
If (service. showdialog (form) = dialogresult. OK)
{
Return form. combobox1.selecteditem;
}
Return value;
}
}
2. Property drop-down editor.
1. In classlibrary, create a new usercontrol (categorydorpdown. CS ).
2. Implement custom uitypeeditor.
3. Because the control in windowform is declared as private in designer. CS, if it is declared as public, the control cannot be referenced in custom uitypeeditor, so we need
ComboBox OBJ =Dropform. controls [2]As ComboBox; you need to find the drop-down control and assign values based on the value parameter, but we need to determine whether the control is null and "".
The value is obj. Text = value as string.
Public class categorydropdowneditor: system. Drawing. Design. uitypeeditor
{
Public categorydropdowneditor ()
{
}
Public override system. Drawing. Design. uitypeeditoreditstyle
Geteditstyle (system. componentmodel. itypedescriptorcontext context)
{
// Specify the editing style as a drop-down shape and based on the control type
Return uitypeeditoreditstyle. dropdown;
}
Public override object editvalue (system. componentmodel. itypedescriptorcontext context,
System. iserviceprovider provider, object value)
{
// Get the editor service object
Iwindowsformseditorservice service =
(Iwindowsformseditorservice) provider. getservice (typeof (iwindowsformseditorservice ));
If (service = NULL)
{
Return NULL;
}
// Define a user control object
String original = string. empty;
String strreturn = string. empty;
Categorydorpdown dropform = new categorydorpdown (service );
ComboBox OBJ = dropform. controls [2] As ComboBox;
If (OBJ! = NULL)
{
If (value! = NULL & value. tostring ()! = "")
{
OBJ. Text = (string) value;
}
}
Service. dropdowncontrol (dropform );
Strreturn = dropform. strreturnvalue;
If (strreturn + String. Empty! = String. Empty)
{
Return strreturn;
}
Return (string) value;
}
}
3.
In this custom property, I inherit stringconverter and return a string array to implement the drop-down:
Code
Public class customcollectionpropertyconverter: stringconverter
{
Public override bool getstandardvaluessupported (itypedescriptorcontext
Context)
{
Return true;
}
Public override bool getstandardvaluesexclusive (itypedescriptorcontext
Context)
{
Return false;
}
Public override standardvaluescollection
Getstandardvalues (itypedescriptorcontext context)
{
String [] strarray = new string [] {"Fruit", "Vegetables", "meat", "Pasta", "eggs "};
Standardvaluescollection returnstandardvaluescollection = new
Standardvaluescollection (strarray );
Return returnstandardvaluescollection;
}
}
Nothing special, but when we rewriteGetstandardvaluesexclusive is true,We cannotDynamic input string, Only according to the followingPull select Value.