Introduction:
In the process of programming, there are always some problems that people cannot solve. Here we have recorded the help of yuanyou to solve the problem, and also provide a little help to friends who encounter the same problem.
Problem description:
Because you need to write a custom ComboBox, You can inherit the original control class and then add the custom function. The problem lies in the process of implementation, but few lines have not been written yet.CodeAnd immediately found a strange phenomenon:
Logically, NO content is added to the items attribute of ComboBox. During the test, the name attribute of the control is added to the items attribute of the design interface.
1. Add the custom control to the toolbox ";
2. Drag the custom control "yecombobox" in the "toolbox" to the form.
(Note:In this step, we will find that the system isIn an instant, The text box shows the default Control name "yecombobox1", and thenQuickly disappearWhen the same control is added for the next testI cannot observe this phenomenon., Maybe the system already hasCache?)
3. view the "items" item in the "properties" of the control. In the "string Set Editor", the default Control name is displayed when an exception occurs in step 1.
Problem code:
The following are all the code for the custom control, which is very small, but I don't know how to solve this problem.
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. drawing; Using System. Data; Using System. text; Using System. Windows. forms; Using System. IO; Namespace Yecontrol { /// <Summary> /// A drop-down box control that can read and save historical records. /// </Summary> [Toolboxbitmap ( Typeof (ComboBox)] partialClass Yecombobox: ComboBox { Public Yecombobox () {initializecomponent ();} /// <Summary> /// Required designer variables. /// </Summary> Private System. componentmodel. icontainer components = Null ; /// <Summary> /// Clear all resources in use. /// </Summary> /// <Param name = "disposing"> If the managed resource should be released, the value is true; otherwise, the value is false. </Param> Protected Override Void Dispose ( Bool Disposing ){ If (Disposing & (components! = Null ) {Components. Dispose ();} Base . Dispose (disposing) ;}# code generated by the region component designer /// <Summary> /// The designer supports the required methods-do not /// Use the code editor to modify the content of this method. /// </Summary> Private Void Initializecomponent () {components = New System. componentmodel. Container (); This . Autocompletesource = system. Windows. Forms. autocompletesource. listitems; This . Allowdrop = True ; This . Autocompletemode = system. Windows. Forms. autocompletemode. Suggest; This . Name =" Yecombobox ";}# EndregionProtected Override Void Ontextchanged (eventargs e ){ Base . Ontextchanged (E ); // It is added to the set only when the drop-down list box does not exist. If(This. Text! =""&&!This. Items. Contains (This. Text ))This. Items. Add (This. Text ); }}}
At this point, the problem occurs in the last section to overwrite the ontextchanged event. Debugging fails because the control is added through the designer. I have also tried to add the control directly to the form through code. No exception was found!
so we are looking for a solution ......