The idea and method of dynamically creating of properties and events in C

  • 2020-05-27 07:03:49
  • OfStack

Usually when writing a program, when you want to use some components, the method 1 is created dynamically and then released. Visual C# can also create components dynamically while the program is running. The following example shows how to use Visual C# to dynamically generate components. First, let's take a look at some of the generalities and theories that are used to dynamically create components.
1. Boxing (crating) and Unboxing (expacking) :
When creating a component dynamically with Visual C#, there are two data type variables involved in the conversion, which are the real value type (Value Type) and the reference type (Reference Type). This conversion process is known in Visual C# as Boxing (boxing) and Unboxing (unboxing). Where the real value type variable is converted to the reference type variable is Boxing (boxing); Converting a reference type variable to a real value type variable is Unboxing (out of the box). So what is the real value type, say some simpler, is that we usually use integer, Boolean, enumeration, and so on, these types of variables are the real value type variable; The reference type in Visual C# refers to Object, Class, Interface, Delegate, String, Array, etc. The main difference between the reference type and the real value type is that the reference type variable holds a pointer to an entity object, while the real value type variable is actually an entity object. In the procedures described in this article, the main concern is the out of the box. Specific processing methods are described below.

2. Key steps in programming and solutions:
The main function of the software in this paper is to create two different types of WinForm components --Button component and TextBox component through two buttons on the form, and assign values to the properties of each component and create events for each component.
1). How to create Button components on a form:
Creating a component using Visual C# is 10 minutes easier, and can be done with the following two lines:


// create 1 A new one Button component  
Button   myButton   =   new   Button   (   )   ; 
// Displays this button on a form  
this.Controls.Add   (   myButton   )   ; 

However, the Button component created at this time does not have any properties and does not have any events. The Button component created in the program described in this article has both properties and events. The following statements are the source code of the Button component created in this program:

// The button count calculator is added after each button is pressed  "1 " 
counter   +=   1   ; 
// The relative position of the ordinate of the button to be generated is forward 1 Plus the vertical position of the relative position of the generated button  "3 " 
locY   +=   this.btnAdd.Height   +   3   ; 
// create 1 A new one Button component  
Button   myButton   =   new   Button   (   )   ; 
// Set his name and Text Property, and the relative position of the generation  
myButton.Name   =   "Button   "   +   counter   ; 
myButton.Text   =   " button    "   +   counter   ; 
myButton.Location   =   new   Point   (   btnAdd.Location.X   ,   locY   )   ;   
// For the generation of new Button Component setting events are set for the generated buttons in this article 3 An event  
myButton.MouseEnter   +=   new   System.EventHandler   (   this.btn_MouseEnter   )   ; 
myButton.MouseLeave   +=   new   System.EventHandler   (   this.btn_MouseLeave   )   ; 
myButton.Click   +=   new   System.EventHandler   (   this.btn_Click   )   ; 
// Displays this button on a form  
this.Controls.Add   (   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 program creates events with the same name for each component. This raises the question of how to identify which Button component triggered the event in the first case.
(2). Determine which component triggered the event:
Since the events for each Button component created in the program are one-like, to properly handle the events for these components, you need to determine which component triggered the event in the event triggering program. This requires the use of the boxed and unboxed items mentioned above. We know that the Sender object is a reference type variable that holds a pointer to the entity object that triggered the current event. To convert it to a real value object type, determine which component triggered the current event with the following statement:

private   void   btn_MouseEnter   (   object   sender   ,   System.EventArgs   e   ) 
{ 
// Out of the box  
Button   currentButton   =   (   Button   )   sender   ; 
// Sets the background color of the button  
currentButton.BackColor   =   Color.Red   ; 
}   

Other events can be handled in a manner that mimics the handling of this event.
(3). How to create TextBox components on a form:
The process of creating an TextBox component is similar to that of creating an Button component, except that there is a difference in the type of component created. The specific implementation statement is as follows:

// The text box number calculator is added after each button is pressed  "1 " 
counter01   +=   1   ; 
// The relative position of the ordinate of the text box to be generated is preceding 1 Plus the vertical position of the relative position of the generated button  "3 
locY1   +=   this.txtAdd.Height   +   3   ; 
// create 1 A new one TextBox component  
TextBox   myBox   =   new   TextBox   (   )   ; 
// Set his name and Text Property, and the location where it was generated  
myBox.Name   =   "TextBox   "   +   counter01   ; 
myBox.Text   =   " The text box    "   +   counter01   ; 
myBox.Location   =   new   Point   (   txtAdd.Location.X   ,   locY1   )   ;   
// For the generation of new TextBox The component sets the event, which is set for the resulting text box in this article 1 An event  
myBox.Click   +=   new   System.EventHandler   (   this.btn_Click   )   ; 
// Displays this text box on a form  
this.Controls.Add   (   myBox   )   ;   

Careful readers will notice that creating Click events for each TextBox component and Click events for Button components are the same. In this way, in Click events, it is not only necessary to determine which component triggered the event, but also which type of component triggered the event.

private   void   btn_Click   (   object   sender   ,   System.EventArgs   e   ) 
{ 
if   (   sender.GetType   (   )   ==   typeof   (   Button   )   )   
{ 
Button   control   =   (   Button   )   sender   ; 
MessageBox.Show   (   control.Text   +   " It's being pushed!  "); 
} 
else   
{ 
TextBox   control   =   (   TextBox   )   sender   ;   
MessageBox.Show   (   control.Text   +   " It's being pushed!  "   )   ; 
} 
}   

Of course, you can also create an Click event for the TextBox component separately. The event statement created at this point can be changed to:

myBox.Click   +=   new   System.EventHandler   (   this.txt   _Click   )   ;   

// Here's the implementation txt   _Click   (   ) Event program code: private   void   txt_Click   (   object   sender   ,   System.EventArgs   e   ) { TextBox   currentButton   =   (   TextBox   )   sender   ; MessageBox.Show   (   currentButton.Text   +   " It's being pushed! "); }

The following is the implementation of the above results of the program source code:


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 the generated button 1 Number calculator  
private   int   counter   ; 
// Define the vertical position of the generated button  
private   int   locY   ; 
// Define the resulting text box 1 Number calculator  
private   int   counter01   ; 
// Define the vertical position of the resulting text box  
private   int   locY1   ; 
public   Form1   (   ) 
{ 
InitializeComponent   (   )   ; 
// Initializes the ordinate position of the generated button and text box  
locY   =   this.btnAdd.Location.Y   ; 
locY1   =   this.txtAdd.Location.Y   ; 
} 

// Clears 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.txtAdd   =   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.txtAdd.FlatStyle   =   FlatStyle.Popup   ; this.txtAdd.Location   =   new   System.Drawing.Point   (   108   ,   16   )   ; this.txtAdd.Name   =   "txtAdd "   ; this.txtAdd.TabIndex   =   1   ; this.txtAdd.Text   =   " Generate text box! "   ; this.txtAdd.Click   +=   new   System.EventHandler   (   this.txtAdd_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   =   " in Visual   C# How to generate components dynamically! "   ; this.ResumeLayout   (   false   )   ;  

} static   void   Main   (   )   { Application.Run   (   new   Form1   (   )   )   ; } private   void   btnAdd_Click   (   object   sender   ,   System.EventArgs   e   ) { // The button count calculator is added after each button is pressed "1 " counter   +=   1   ; // The relative position of the ordinate of the button to be generated is forward 1 Plus the vertical position of the relative position of the generated button "3 " locY   +=   this.btnAdd.Height   +   3   ; // create 1 A new one Button component Button   myButton   =   new   Button   (   )   ; // Set his name and Text Property, and the location where it was generated myButton.Name   =   "Button   "   +   counter   ; myButton.Text   =   " button    "   +   counter   ; myButton.Location   =   new   Point   (   btnAdd.Location.X   ,   locY   )   ;  

// For the generation of new Button Component setting events are set for the generated buttons in this article 3 An event myButton.MouseEnter   +=   new   System.EventHandler   (   this.btn_MouseEnter   )   ; myButton.MouseLeave   +=   new   System.EventHandler   (   this.btn_MouseLeave   )   ; myButton.Click   +=   new   System.EventHandler   (   this.btn_Click   )   ; // Displays this button on a form this.Controls.Add   (   myButton   )   ; }

private   void   txtAdd_Click   (   object   sender   ,   System.EventArgs   e   ) { // The text box number calculator is added after each button is pressed "1 " counter01   +=   1   ; // The relative position of the ordinate of the text box to be generated is preceding 1 Plus the vertical position of the relative position of the generated button "3 locY1   +=   this.txtAdd.Height   +   3   ; // create 1 A new one TextBox component TextBox   myBox   =   new   TextBox   (   )   ; // Set his name and Text Property, and the location where it was generated myBox.Name   =   "TextBox   "   +   counter01   ; myBox.Text   =   " The text box    "   +   counter01   ; myBox.Location   =   new   Point   (   txtAdd.Location.X   ,   locY1   )   ;   // For the generation of new TextBox The component sets the event, which is set for the resulting text box in this article 1 An event myBox.Click   +=   new   System.EventHandler   (   this.btn_Click   )   ; // Displays this text box on a form this.Controls.Add   (   myBox   )   ; } private   void   btn_MouseEnter   (   object   sender   ,   System.EventArgs   e   ) { // Out of the box Button   currentButton   =   (   Button   )   sender   ; // Sets the background color of the button currentButton.BackColor   =   Color.Red   ; }

private   void   btn_MouseLeave   (   object   sender   ,   System.EventArgs   e   ) { // Out of the box 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   +   " It's being pushed! "); } else   { TextBox   control   =   (   TextBox   )   sender   ;   MessageBox.Show   (   control.Text   +   " It's being pushed! "   )   ; } } } }  


4. Conclusion:
From the above introduction, it is not difficult to see that creating a component dynamically is not a difficult thing. The difficulty lies in creating an event for this component, because it involves the conversion of real value type variables and reference type variables, which is known as the problem of boxing and unboxing. Of course, at programming time, you can create not only visible components, but also invisible components, implemented in a manner similar to the one described in this article.


Related articles: