![]() |
Introduction to Windows Controls |
The objects used in a Windows application are defined in various assemblies. To use one of these controls in your application, you must first know the name of its class. With this information, you can declare a variable of its class. For example, a command button is an object of type Button that is based on the Button class. The Button class is defined in the System.Windows.Forms namespace of the System.Windows.Forms.dll assembly. Based on this, to create a button, you can declare a Button variable as follows: using System; using System.Windows.Forms; class Program : Form { static void Main() { Button btnSubmit; } } After, or when, declaring the variable, you can use the new operator to allocate memory for it: using System; using System.Windows.Forms; class Program : Form { static void Main() { Button btnSubmit; btnSubmit = new Button(); } } This is also referred to as dynamically creating a control. After declaring the variable and allocating memory for it, the control is available but doesn't have a host, which makes it invisible. You can then call the Controls property of its parent to add the new control to its collection. The parent would then be responsible for hosting or carrying the new control. Here is an example: using System; using System.Windows.Forms; class Program : Form { static void Main() { Program frm; Button btnSubmit; frm= new Exercise(); btnSubmit = new Button(); frm.Controls.Add(btnSubmit); } } If you declare and completely create a control in a particular method, the control would be available only in that method and cannot be accessed outside. To make such a control available to other controls or methods of the same class, you should declare it outside of a method. You can do this by declaring the variable in the form's class. Here is an example: using System; using System.Windows.Forms; class AppMainForm : Form { private Button btnSubmit; static void Main() { } } After declaring the control, you can allocate memory for it in the form's constructor or prior to using it. Here is an example: using System; using System.Windows.Forms; class AppMainForm : Form { private Button btnSubmit; public AppMainForm() { btnSubmit = new Button(); this.Controls.Add(btnSubmit); } static void Main() { } } Because there can be many controls used in a program, instead of using the constructor to initialize them, you can create a method called InitializeComponent to initialize the various objects used in your application. Then simply call that method from the constructor of your form.
If you are using a .NET Framework control, you must know the name of the class on which the control is based (and each control is based on a particular class). If you have examined the types of classes available but none of them implements the behavior you need, you can first locate one that is close to the behavior you need, then use it as a base to derive a new class. To derive a class from an existing control, you can use your knowledge of inheritance to derive a new class. Here is an example: public class Numeric : System.Windows.Forms.TextBox { } If you want to perform some early initialization to customize your new control, you can declare a constructor. Here is an example: public class Numeric : System.Windows.Forms.TextBox { public Numeric() : base() { } } Besides the constructor, in your class, you can add the properties, methods, and/or events as you see fit. You can also use it to globally set a value for a property of the base class. Once the control is ready, you can dynamically use it like any other control. Here is an example:
|
|
||
Previous | Copyright © 2004-2012, FunctionX | Next |
|