Use Visual C # To dynamically generate Components
Previously written in Delphi Program In this case, you do not like to emit many components on the form. This is not very nice, and it is not very convenient to debug the program. When writing a program, when some components are used, they are generally created dynamically and released after use. Visual C # You can also dynamically create components when running the program. The following uses a program example to describe how to use Visual C # To dynamically generate components. First, let's take a look at some introductions and theories that will be used in the process of dynamically creating components.
I. Boxing and unboxing ):
When using Visual C # To dynamically create a component, two types of data type variables are involved. These two types of variables are value types) variable and reference type variable, which are called boxing and unboxing in Visual C ). Convert the real value type variable to the reference type variable boxing (boxed). Convert the reference type variable to the actual value type variable unboxing (unboxing ). So what is the real value type? Simply put, we usually use integer, Boolean, and enumeration types. These types of variables are real value type variables. The so-called reference type, in Visual C #, object, class, interface, Delegate, String, array, etc. The main difference between them and actual value types is, reference type variables store pointers to object objects, while real-value variables are actually object objects. In the program described in this article, it mainly involves getting out of the box. The specific processing method is described below.
2. The environment for program design and operation in this article:
(1). Microsoft Windows 2000 Server Edition
(2). Net Framework SDK beta 2
3. Key Steps and solutions in programming:
The main function of the software in this article is to use two buttons on the form to create two different types of winform components-the button component and the textbox component, and assign values to the attributes of each component while creating the component, an event is also created for each created component.
(1). How to create the button component on the form:
In fact, it is very convenient to use Visual C # To create a component. Only the following two rows of statements can be used:
// Create a new button component
Button mybutton = new button ();
// Display this button in the form
This. Controls. Add (mybutton );
However, the button component created at this time has no attributes and no events. The button component created in the program described in this article has not only attributes but also events, the following statements are used to create the button component in this document. Source code :
// The button quantity calculator adds "1" after each button is pressed"
Counter + = 1;
// Add "3" to the vertical coordinate of the button to be generated"
Locy + = This. btnadd. height + 3;
// Create a new button component
Button mybutton = new button ();
// Set its name and text attributes and the relative location
Mybutton. Name = "button" + counter;
Mybutton. Text = "button" + counter;
Mybutton. Location = new point (btnadd. Location. X, locy );
// Set events for the generated new button component. In this article, three events are set for the generated button.
Mybutton. mouseenter + = new system. eventhandler (this. btn_mouseenter );
Mybutton. mouseleave + = new system. eventhandler (this. btn_mouseleave );
Mybutton. Click + = new system. eventhandler (this. btn_click );
// Display this button in the form
This. Controls. Add (mybutton );
The program not only assigns values to the attributes of each component, but also creates three events for each component. Careful readers may have noticed that the names of events created by a program for each component are the same. In this way, there is a question about how to identify which button component triggers the event in the same event.
(2) determine which component triggers the event:
The events of each button component created in the program are the same. to correctly handle the events of these components, You need to determine which component triggers the event in the program triggered by the event. In this case, we need to use the packing and picking methods mentioned above. We know that the sender object is a reference type variable, which stores a pointer to the object that triggers the current event. To convert it to a real-value object type, you can use the following statement to determine which component triggers the current event:
Private void btn_mouseenter (Object sender, system. eventargs E)
{
// Export
Button currentbutton = (button) sender;
// Set the background color of the button
Currentbutton. backcolor = color. Red;
}
Other events can be handled in the same way as the event handling process.
(3) how to create a textbox component on a form:
The process of creating a textbox component is similar to that of creating a button component, but there is a difference in the type of the created component. The specific implementation statement is as follows:
// The number of text boxes. The calculator adds "1" after each button is pressed"
Counter01 + = 1;
// Add "3" to the vertical coordinate of the text box to be generated.
Locy1 + = this.txt Add. height + 3;
// Create a new textbox component
Textbox mybox = new Textbox ();
// Set its name and text attributes and the generated location
Mybox. Name = "textbox" + counter01;
Mybox. Text = "text box" + counter01;
Mybox. Location = new point (txtadd. Location. X, locy1 );
// Set events for the generated new textbox component. In this article, an event is set for the generated textbox.
Mybox. Click + = new system. eventhandler (this. btn_click );
// Display this text box in the form
This. Controls. Add (mybox );
At this time, the readers will find that the click events created for each textbox component and the click events created for the button component are the same, in this way, the click event not only determines which component triggers the event, but also determines which type of component triggers the event. The following statement is a specific method to implement these judgments:
Private void btn_click (Object sender, system. eventargs E)
{
If (sender. GetType () = typeof (button ))
{
Button control = (button) sender;
MessageBox. Show (control. Text + "pressed! ");
}
Else
{
Textbox Control = (textbox) sender;
MessageBox. Show (control. Text + "pressed! ");
}
}
Of course, you can also create a click event for the textbox component separately. The event statement can be changed:
Mybox. Click + = new system. eventhandler (this.txt _ click );
The following is a program that implements the TXT _ click () event. Code :
Private void txt_click (Object sender, system. eventargs E)
{
Textbox currentbutton = (textbox) sender;
MessageBox. Show (currentbutton. Text + "is pressed! ");
}
4. Interface on which the source program has been run in this article:
The following figure shows the program running:
Figure 01: dynamically created components in the program
Figure 02: result chart of the created button
Figure 03: result chart of the created text box
The source code of the program that implements the above result is as follows:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Namespace dynamiccontrols
{
Public class form1: Form
{
Private button btnadd;
Private system. componentmodel. Container components = NULL;
Private button txtadd;
// Define a quantity Calculator For the generated button
Private int counter;
// Define the vertical coordinates of the relative positions for the generated button
Private int locy;
// Define a quantity Calculator For the generated text box
Private int counter01;
// Define the vertical coordinates of the relative positions for the generated text box
Private int locy1;
Public form1 ()
{
Initializecomponent ();
// Initialize the Y coordinate of the text box location of the generated button
Locy = This. btnadd. Location. Y;
Locy1 = this.txt Add. Location. Y;
}
// Clear the resources used in the program
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void initializecomponent ()
{
This. btnadd = new button ();
This.txt add = new button ();
This. suspendlayout ();
This. btnadd. flatstyle = flatstyle. Popup;
This. btnadd. Location = new system. Drawing. Point (8, 16 );
This. btnadd. Name = "btnadd ";
This. btnadd. tabindex = 0;
This. btnadd. Text = "generate button! ";
This. btnadd. Click + = new system. eventhandler (this. btnadd_click );
This.txt Add. flatstyle = flatstyle. Popup;
This.txt Add. Location = new system. Drawing. Point (108, 16 );
This.txt Add. Name = "txtadd ";
This.txt Add. tabindex = 1;
This.txt Add. Text = "generate text box! ";
This.txt Add. Click + = new system. eventhandler (this.txt add_click );
This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (292,273 );
This. Controls. Add (btnadd );
This. Controls. Add (txtadd );
This. Name = "form1 ";
This. Text = "How to dynamically generate components in Visual C! ";
This. resumelayout (false );
}
Static void main ()
{
Application. Run (New form1 ());
}
Private void btnadd_click (Object sender, system. eventargs E)
{
// The button quantity calculator adds "1" after each button is pressed"
Counter + = 1;
// Add "3" to the vertical coordinate of the button to be generated"
Locy + = This. btnadd. height + 3;
// Create a new button component
Button mybutton = new button ();
// Set its name and text attributes and the generated location
Mybutton. Name = "button" + counter;
Mybutton. Text = "button" + counter;
Mybutton. Location = new point (btnadd. Location. X, locy );
// Set events for the generated new button component. In this article, three events are set for the generated button.
Mybutton. mouseenter + = new system. eventhandler (this. btn_mouseenter );
Mybutton. mouseleave + = new system. eventhandler (this. btn_mouseleave );
Mybutton. Click + = new system. eventhandler (this. btn_click );
// Display this button in the form
This. Controls. Add (mybutton );
}
Private void txtadd_click (Object sender, system. eventargs E)
{
// The number of text boxes. The calculator adds "1" after each button is pressed"
Counter01 + = 1;
// Add "3" to the vertical coordinate of the text box to be generated.
Locy1 + = this.txt Add. height + 3;
// Create a new textbox component
Textbox mybox = new Textbox ();
// Set its name and text attributes and the generated location
Mybox. Name = "textbox" + counter01;
Mybox. Text = "text box" + counter01;
Mybox. Location = new point (txtadd. Location. X, locy1 );
// Set events for the generated new textbox component. In this article, an event is set for the generated textbox.
Mybox. Click + = new system. eventhandler (this. btn_click );
// Display this text box in the form
This. Controls. Add (mybox );
}
Private void btn_mouseenter (Object sender, system. eventargs E)
{
// Export
Button currentbutton = (button) sender;
// Set the background color of the button
Currentbutton. backcolor = color. Red;
}
Private void btn_mouseleave (Object sender, system. eventargs E)
{
// Export
Button currentbutton = (button) sender;
Currentbutton. backcolor = control. defaultbackcolor;
}
Private void btn_click (Object sender, system. eventargs E)
{
If (sender. GetType () = typeof (button ))
{
Button control = (button) sender;
MessageBox. Show (control. Text + "pressed! ");
}
Else
{
Textbox Control = (textbox) sender;
MessageBox. Show (control. Text + "pressed! ");
}
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service