Zhengzuo
2004-11-30
People who have done ASP.NET know that it is very convenient to use user controls when developing, providing considerable flexibility for functional modularization. It is a happy to develop a Windows form or you can use user controls. Here we take a look at adding properties and events for user controls and implement messages to the parent container. This article is mainly to provide some reference for friends who have not used user controls.
The implementation of the user control is relatively simple, from System.Windows.Forms.userControl.
Public Class UserControl1: System.Windows.Forms.userControl
In order to facilitate test I added a TextBox, and register the TextChange Event of TextBox,
THIS.TEXTBOX1.TEXTCHANGED = New System.EventHandler (this.textbox1_textchanged);
Event handler,
Private void textbox1_textchanged (Object Sender, System.Eventargs E)
{
Messagebox.show (THIS.TEXTBOX1.TEXT);
}
Here Demonstrate If the content change of the text box in the control will use MessageBox to display the current text box content.
The control is shown below:
Add the above user control in the form, when we change the textBox text, you can see a dialog box, it is very simple.
Let's take a look at the added properties of the control.
A private variable is defined here.
PRIVATE STRING CUSTOMVALUE;
Add access to his properties
Public String CustomValue
{
Get {returnj
SET {CustomValue = Value;
}
Access like ordinary controls when used in the form,
UserControl11.customValue = "User Control Custom Data";
By events can pass messages to the form, let's write a simple parameter class before defining.
Public Class TextChangeEventArgs: Eventargs
{
PRIVATE STRING MESSAGE;
Public TextChangeeventargs (String Message)
{
THIS.MESSAGE = Message;
}
Public String Message
{
Get {returnome;}
}
}
Define commissioning,
Public Delegate Void TextBoxChangedHandle (Object Sender, TextChangeEventArgs E);
Next, add an event in the user control,
/ / Define events
Public Event TextBoxChangedhandle UserControlValueChanged;
In order to stimulate the new event of the user control, the code is modified.
Private void textbox1_textchanged (Object Sender, System.Eventargs E)
{
UserControlValueChanged! = NULL)
UserControlValueChanged (this, new textchangeeventargs (THISTEXTBOX1.TEXT)
}
Ok, in order to make it easy to answer questions on CSDN, put the complete code:
Using system;
Using system.collections;
Using system.componentmodel;
Using system.drawing;
Using system.data; using system.windows.forms;
Namespace zz.WindowsApplication1
{
Public Class UserControl1: System.Windows.Forms.userControl
{
Private system.windows.Forms.TextBox textBox1;
PRIVATE STRING CUSTOMVALUE;
Private system.componentmodel.Container Components = NULL;
Public String CustomValue
{
Get {returnj
SET {CustomValue = Value;
}
/ / Define events
Public Event TextBoxChangedhandle UserControlValueChanged;
Public userControl1 ()
{
InitializationComponent ();
}
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#REGION component designer generated code
Private vidinitiRizeComponent ()
{
This.TextBox1 = new system.windows.Forms.TextBox ();
THIS.SUSPENDLAYOUT ();
This.TextBox1.Location = new system.drawing.point (12, 36);
THIS.TEXTBOX1.NAME = "TextBox1";
this.TextBox1.tabindex = 0;
THIS.TEXTBOX1.TEXT = "TextBox1";
THIS.TEXTBOX1.TEXTCHANGED = New System.EventHandler (this.textbox1_textchanged);
This.Controls.add (this.TextBox1);
This.name = "UserControl1";
This.size = new system.drawing.size (150, 92);
This.ResumeLayout (false);
}
#ndregion
Private void textbox1_textchanged (Object Sender, System.Eventargs E)
{
UserControlValueChanged! = NULL)
UserControlValueChanged (this, new textchangeeventargs (THISTEXTBOX1.TEXT)
}
}
// Define commission
Public Delegate Void TextBoxChangedHandle (Object Sender, TextChangeEventArgs E);
Public Class TextChangeEventArgs: Eventargs
{
PRIVATE STRING MESSAGE;
Public TextChangeeventargs (String Message)
{
THIS.MESSAGE = Message;
}
Public String Message
{
Get {returnome;}
}
}
}
When using it, register the above event during use, more simply put the source code, use system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
Namespace zz.WindowsApplication1
{
Public Class Form1: System.Windows.Forms.form
{
Private windowsapplication1.userControl1 UserControl11;
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
InitializationComponent ();
UserControl11.customValue = "User Control Custom Data";
UserControl11.userControlValueChanged = New TextBoxChangeDHandle (userControl11_userControlValueChanged);
}
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
Private vidinitiRizeComponent ()
{
THIS.UserControl11 = New windowSapplication1.userControl1 ();
THIS.SUSPENDLAYOUT ();
This.userControl11.Location = new system.drawing.point (8, 8);
This.userControl11.Name = "UserControl11";
This.userControl11.size = new system.drawing.size (150, 84);
THIS.UserControl11.tabindex = 0;
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
THIS.CLIENTSIZE = New System.drawing.size (292, 193);
This.Controls.add (this.userControl11);
THIS.NAME = "Form1";
THIS.TEXT = "Form1";
This.ResumeLayout (false);
}
#ndregion
[Stathread]
Static void
Main
()
{
Application.run (New Form1 ());
}
Private Void UserControl11_UserControlValueChanged (Object Sender, TextChangeeeventargs E)
{
Messagebox.show ("The value of the current control is:" E.MESSAGE);
}
}
}
In addition, you need to dynamically load, add the control to the container's Controls collection, and below is added to the constructor,
Public Form1 ()
{
InitializationComponent (); userControl1 uc = new userControl1 ();
Uc.customvalue = "Dynamic Load User Control";
Uc.userControlValueChanged = new textboxchangedhandle (userControl11_userControlValueChanged);
This.Controls.Add (UC);
}
In addition, dragging the user control into the form from the toolbox in the VS.NET, if it is the first time you need to compile a project.