namespace myApplication {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
What is partial class Form1 : Form
and what is InitializeComponents();
and why it is used Form1 : Form
instead of only Form1
InitializeComponents();
is a function for initializing the values of the form. Please right click on it and click on Showdefinition to see its contents. It is used to assign values to labels, textbox, Buttons etc in your form.
public partial class Form1 : Form
By using partial
it is possible to write the definition of same class in two different source file in the same namespace.It will be treated as same during compilation.You can find a class with same name Form1
in your project which is created automatically.
Form1 is the name of the Form and :
is used to inherit the properties of base class . Here Form
represents System.Windows.Forms.Form
. We are inheriting to access the properties and methods of base class.
Let's look at partial class Form1 : Form
- obviously this is declaring a class
called Form1
. The : Form
part means that it inherits from the base class Form
.
The base Form
class provides lots of functionality needed by all forms - including InitializeComponents()
(more on which below).
The partial
keyword means that this class is only partly defined in this source file, and the compiler must look elsewhere for the remainder of the definition (in this case it will be some automatically generated code which defines amongst others all the controls you placed on your form in the designer).
InitializeComponents()
is a required call in all subclasses of Form
and it instructs all the components on the form to initialise, position, and display themselves as appropriate.
- Class declaration
- Method call
- Inheritance
But asking questions like this is no good.
精彩评论