I'm following this tutorial on winforms, and so far the tutorial is coding the form without using the toolbox. I believe it'll introduce the toolbox in more depth shortly.
Following the tutorial, I've made a partial class in the following two pieces of code:
First file:
using System;
using System.Windows.Forms;
public class Numeric : System.Windows.Forms.TextBox
{
public Numeric()
{
}
}
public partial class Exercise
{
private Numeric txtbox;
System.ComponentModel.Container components;
}
Second file:
using System;
using System.Windows.Forms;
public partial class Exercise : Form
{
private void InitializeComponent()
{
txtbox = new Numeric();
Controls.Add(txtbox);
}
public Exercise()
{
InitializeComponent();
}
}
public class program
{
public static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
When I run the code with F5, everything looks fine: The form pops up with the textbox.
But for some reason, when I right-click on the second file and choose "View Designer", I get an error which says "The variable 'txtbox' is either undeclared or was never assigned". I can choose to "Ignore and Continue", which leads me to a form with no textbox on it.
Why does this occur? I know some of you think I should just use the toolbox, and it's probably the most sensible thing to do, but I would still like to understand why this happens.
The declaration of the controls should be put into Designer.cs files in order Visual Studio can just compile this unit and display it.
When you launch the app, the compiler takes into account all parts of your partial class then it finds txtBox declaration.
Try leave only the form class with its graphical declarations in a single file. This single file should have InitializeComponent methdod, construcutor and field declarations of UI components initialized in InitializeComponent().
When you open a
Form
in windows forms designer, the designer looks into the first class in the file. If the file has aDesigner.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 andInitializeComponents
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:
Form
will not execute at design-time, but the constructor of base class of your form will execute in design-time.InitializeComponent
will not execute at design-time, but those codes will be deserialized and will be used to create designer of the form.MyForm:SomeForm<SomeClass>
, but it can showSomeForm<T>:Form
. (solution)Example
Take a look at below code, which has some serious problems:
int i="x";
InitializeComponent
method didn't call in constructorBut 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:
And here is screenshot of designer:
More information
To find more information, take a look at this link:
Solution for your question
As a solution, it is enough for you to move
private Numeric txtbox;
and put it your seccond file inExercise
class.