In the "Write Form Designer Attempt ( 3. ) Add a widget to the form. Leejee The problem of custom controls is raised. I did a small test to implement the use of custom controls in the designer.
You must first prepare a custom control. Create Windows Control Scheme, named Mycontrol, Add Comboboxfield User Control, put Label And one ComboBox, Generate a solution. The generated DLL Copy files to the test directory D: \ DOTNET .
Open the designer solution and add Mycontrol. dll InCodeAdd Using mycontrol; Then Toolboxservice. toolbox. Items. Add ( ... .);Add a line of code:
Toolboxservice. toolbox. Items. Add (NewToolboxitem (Typeof(Mycontrol. comboboxfield )));
you can use this custom control just like other standard controls. Hey, there was no accident, but it was as simple as the previous experiment.
but if you think about the problem, we need to add a reference to the file where the control is located in the project, you need to write the Class Name of the control in the code. That is to say, when writing a designer, we need to know which custom controls we want to use. In vs , vs we don't know what we're going to add in advance. What should I do to implement this function? Naturally, we must use "reflection ".
Delete the reference we just added in the project, and delete the two statements we just wrote in the main form code.
write the second statement in front of it:
Assembly A1=Assembly. loadfrom (@"D: \ DOTNET \ mycontrol. dll");
Toolboxservice. toolbox. Items. Add (NewToolboxitem (a1.gettype ("Mycontrol. comboboxfield")));
the running solution has the same effect as the previous one. It seems that the dynamic library file name and type name are also hardcoded to the code, but the two strings can be easily read from the configuration file, in this way, you can use custom controls when using the designer .
Related chapters:
WriteFormDesigner Attempt(I) Start
WriteFormDesigner Attempt(II) Propertygrid
WriteFormDesigner Attempt(3.)Add controls to a form
Try to write the Form Designer (4) modify the control attributes on the form
Note: I wrote this article last night. When I went to bed, I thought there was something wrong. That is, I used reflection before loading the toolbox, I guess vs uses reflection only when this control is needed. The solution to this problem is to modify toolboxservice. I will implement this function in a more comprehensive version in the future. 2005/11/07