Usually when writing programs, when certain components are used, the methods used are generally dynamically created and released after use. Visual C# can also create components dynamically when the program is running. The following is a program example to introduce how to use Visual C# to dynamically generate components. First, let us understand some introductions and theories to be used in the process of dynamically creating components.
one. Boxing (packing) and Unboxing (out of the box):
When creating components dynamically with Visual C#, conversion of two types of variables is involved. These two type variables are real-value type (Value Type) variables and reference type (Reference Type) variables. This conversion process is called Boxing (packing) and Unboxing (outboxing) in Visual C#. Among them, converting real-valued type variables into reference type variables is Boxing (packing); converting reference type variables into real-valued type variables is Unboxing (packing). So what is a real-valued type? To put it simply, it is the integer, boolean, enumerated type, etc. we usually use. These types of variables are real-valued type variables; the so-called reference type refers to Object, Class, Interface, Delegate, String, Array, etc. In Visual C#, the most important difference between it and real-valued type is that the reference type variable stores a pointer to an entity object, while the real-valued type variable is a real entity object. In the program introduced in this article, the main thing involved is to get out of the box. The specific treatment methods are described in detail below.
two. Key steps and solutions in programming:
The main function of the software in this article is to create two different types of WinForm components - Button components and TextBox components through two buttons on the form, and assign values to the attributes of each component while creating it, creating events for each created component.
1).How to create a Button component on a form:
In fact, it is very convenient to create a component with Visual C#. It can be completed with just the following two lines:
//Create a new Button component
Button myButton = new Button ( ) ;
//Show this button in the form
( myButton ) ;
However, the Button component created at this time has no properties and no events. The Button component created in the program introduced in this article not only has properties but also events. The following statement is the source code of the Button component created by this program:
//The button count calculator adds "1" after each button press
counter += 1 ;
//The relative position of the vertical position of the button to be generated is the vertical position of the previous button to be generated is the vertical position of the button to be generated.
locY += + 3 ;
//Create a new Button component
Button myButton = new Button ( ) ;
//Set its name and Text attributes, as well as the relative position of the generated
= "Button " + counter ;
= "Button " + counter ;
= new Point ( , locY ) ;
// Set events for the generated new Button component. In this article, three events are set for the generated button.
+= new ( this.btn_MouseEnter ) ;
+= new ( this.btn_MouseLeave ) ;
+= new ( this.btn_Click ) ;
//Show this button in the form
( myButton ) ;
The program not only assigns values to the properties 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. This has a problem, how to identify which Button component triggered the event in this same event.
(2). Determine which component triggered the event:
Since the events of the Button component created for each created in the program are the same, in order to correctly handle the events of these components, you need to determine which component triggered the event in the program triggered by the event. This requires the use of the packing and outgoing mentioned above. We know that the Sender object is a reference type variable, which stores a pointer to the entity object that triggers the current event. To convert it to a real-valued object type, you can determine which component triggers the current event through the following statement:
private void btn_MouseEnter ( object sender , e )
{
//Out of the box
Button currentButton = ( Button ) sender ;
//Set the background color of the button
= ;
}
Other events can be processed according to the processing of this event.
(3). How to create a TextBox component on a form:
The process of creating a TextBox component is similar to the process of creating a Button component, except that there is a little difference in the type of component created. The specific implementation statement is as follows:
//The text box count calculator adds "1" after each button press
counter01 += 1 ;
//The relative position of the vertical position of the text box to be generated is the relative position of the previous generation button plus "3
locY1 += + 3 ;
//Create a new TextBox component
TextBox myBox = new TextBox ( ) ;
//Set its name and Text attributes, as well as its generated location
= "TextBox " + counter01 ;
= "Text box " + counter01 ;
= new Point ( , locY1 ) ;
// Set an event for the generated new TextBox component. In this article, an event is set for the generated text box.
+= new ( this.btn_Click ) ;
//Show this text box in the form
( myBox ) ;
At this time, careful readers will find that creating a Click event for each TextBox component is the same as the Click event created for the Button component. In this way, in the Click event, you must not only determine which component triggers the event, but also determine which type of component triggers the event. The following statement is the specific method to implement these judgments:
private void btn_Click ( object sender , e )
{
if ( ( ) == typeof ( Button ) )
{
Button control = ( Button ) sender ;
( + "Pressed!");
}
else
{
TextBox control = ( TextBox ) sender ;
( + "Pressed!" ) ;
}
}
Of course, if you can also create Click events for the TextBox component separately. The event statement created at this time can be changed to:
+= new ( _Click ) ;
//The following is the program code that implements the txt _Click ( ) event:
private void txt_Click ( object sender , e )
{
TextBox currentButton = ( TextBox ) sender ;
( + "Pressed!");
}
Here is the program source code that implements the above results:
using System ;
using ;
using ;
using ;
using ;
using ;
namespace DynamicControls
{
public class Form1 : Form
{
private Button btnAdd ;
private components = null ;
private Button txtAdd ;
//Define a quantity calculator for the generated button
private int counter ;
//Define the vertical coordinate of the relative position 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 position for the generated text box
private int locY1 ;
public Form1 ( )
{
InitializeComponent ( ) ;
//Initialize the vertical coordinate of the generated button and the text box position of the generated button
locY = ;
locY1 = ;
}
//Clear the resources used in the program
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
( ) ;
}
}
( disposing ) ;
}
private void InitializeComponent ( )
{
= new Button ( ) ;
= new Button ( ) ;
( ) ;
= ;
= new ( 8 , 16 ) ;
= "btnAdd " ;
= 0 ;
= "Generate button!" ;
+= new ( this.btnAdd_Click ) ;
= ;
= new ( 108 , 16 ) ;
= "txtAdd " ;
= 1 ;
= "Generate text box!" ;
+= new ( this.txtAdd_Click ) ;
= new ( 5 , 13 ) ;
= new ( 292 , 273 ) ;
( btnAdd ) ;
( txtAdd ) ;
= "Form1 " ;
= "How to dynamically generate components in Visual C#!" ;
( false ) ;
}
static void Main ( )
{
( new Form1 ( ) ) ;
}
private void btnAdd_Click ( object sender , e )
{
//The button count calculator adds "1" after each button press
counter += 1 ;
//The relative position of the vertical position of the button to be generated is the vertical position of the previous button to be generated is the vertical position of the button to be generated.
locY += + 3 ;
//Create a new Button component
Button myButton = new Button ( ) ;
//Set its name and Text attributes, as well as its generated location
= "Button " + counter ;
= "Button " + counter ;
= new Point ( , locY ) ;
// Set events for the generated new Button component. In this article, three events are set for the generated button.
+= new ( this.btn_MouseEnter ) ;
+= new ( this.btn_MouseLeave ) ;
+= new ( this.btn_Click ) ;
//Show this button in the form
( myButton ) ;
}
private void txtAdd_Click ( object sender , e )
{
//The text box count calculator adds "1" after each button press
counter01 += 1 ;
//The relative position of the vertical position of the text box to be generated is the relative position of the previous generation button plus "3
locY1 += + 3 ;
//Create a new TextBox component
TextBox myBox = new TextBox ( ) ;
//Set its name and Text attributes, as well as its generated location
= "TextBox " + counter01 ;
= "Text box " + counter01 ;
= new Point ( , locY1 ) ;
// Set an event for the generated new TextBox component. In this article, an event is set for the generated text box.
+= new ( this.btn_Click ) ;
//Show this text box in the form
( myBox ) ;
}
private void btn_MouseEnter ( object sender , e )
{
//Out of the box
Button currentButton = ( Button ) sender ;
//Set the background color of the button
= ;
}
private void btn_MouseLeave ( object sender , e )
{
//Out of the box
Button currentButton = ( Button ) sender ;
= ;
}
private void btn_Click ( object sender , e )
{
if ( ( ) == typeof ( Button ) )
{
Button control = ( Button ) sender ;
( + "Pressed!");
}
else
{
TextBox control = ( TextBox ) sender ;
( + "Pressed!" ) ;
}
}
}
}
Four. Summarize:
Through the above introduction, it is not difficult to see that dynamically creating components is not a very difficult thing. The difficulty lies in creating events for this component, because this involves the conversion of real-valued type variables and reference type variables, which is the so-called boxing and out-of-boxing problem. Of course, when designing programming, you can not only create visible components, but also invisible components. The specific implementation method is similar to the methods in this article.