How to use custom constructor in WinForm?

I need to create a Winform instance in another project. How it's done? I'm currently trying to bind a default constructor. It seems that my custom constructor is not being called.

Also .. the entry point for this application will not be in the project that owns this form. The value below will not execute:

Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new HtmlTestForm()); 

I'm not quite sure what this code does. Will the form work?

 private HtmlTestForm() { InitializeComponent(); OpenBrowser(new Uri(TestURL)); } public HtmlTestForm(Uri uri) :this() { TestURL = uri; } 

// New form in another project.

 HtmlTestForm form = new HtmlTestForm(new Uri("http://SomeUri.html")); 
+6
c # winforms
source share
1 answer

The form will work.
However, TestURL will only be assigned after calling OpenBrowser . ( : this() will execute the entire default constructor first)

Instead, you should probably call InitializeComponent separately in your custom constructor, not the default chaining.

. Pure class classes are ordinary classes that contain an automatically generated method called InitializeComponent .
They don't have the magic you need to know about (unlike VB6); as long as you understand the purpose of the InitializeComponent (read its source), you can do whatever you want with it.

+8
source share

All Articles