Try to write the Form Designer (5) to make the designer use custom controls

Source: Internet
Author: User
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

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.