How does the Windows Forms Designer work?
When you open a Form
in windows forms designer, the designer looks into the first class in the file. If the file has a Designer.cs
containing the other partial part of the class, also includes it and tries to deserialize those file contents. In the process of deserialization and loading the design time of your form, it creates an instance of the base class of your form and looks in those files for component declarations and InitializeComponents
method. If find them creates components and sets properties of them using deserialized codes and add components to the instance of base class which created.
Some useful facts:
- Codes in constructor of your
Form
will not execute at design-time, but the constructor of base class of your form will execute in design-time. - Codes in
InitializeComponent
will not execute at design-time, but those codes will be deserialized and will be used to create designer of the form. - The designer can not show a form which has an abstract base class. (solution)
- The designer can not show a form which has generic class. For example it can not show
MyForm:SomeForm<SomeClass>
, but it can showSomeForm<T>:Form
. (solution) - If you define a new property for your form, the properties will not show in properties window. The properties window, shows the properties of base class but with values of your form.
- When a file contains 2 class, if the form was not the first class the designer can not load and you receive a warning that says the form should be first class to show in designer.
- Above rules will apply also to UserControls.
Example
Take a look at below code, which has some serious problems:
- The class has different constructor than class name
- The statement
int i="x";
- There is no semicolons while this is a C# class
- The
InitializeComponent
method didn’t call in constructor
But the interesting news is you can see the form in designer, even with those errors!
Just create a file in your project and put below codes in the file and save the file and close it. Then without trying to build the solution, open the form in designer. Here is code:
using System
using System.Windows.Forms
namespace SampleApplication
{
public class MyForm:Form
{
public NotMyForm()
{
}
public void InitializeComponent()
{
int i="x";
textBox1 = new TextBox()
textBox1.Text = "Hi"
this.Controls.Add(textBox1)
}
private TextBox textBox1
}
}
And here is screenshot of designer:
More information
To find more information, take a look at this link:
- How does the Windows Forms designer in Visual Studio load a Form? (The original website is off – You can find the archive here)
Solution for your question
As a solution, it is enough for you to move private Numeric txtbox;
and put it your seccond file in Exercise
class.