Applications in. netProgramUse User Controls
People who have worked on ASP.net know that it is very convenient to use user controls during development, providing considerable flexibility for functional modularization. Fortunately, you can also use user controls to develop Windows Forms. Here we will look at adding properties and events for the user control and sending messages to the parent container. This article provides some reference for friends who have never used user controls.
The implementation of user controls is relatively simple, directly from system. Windows. Forms. usercontrol.
Public class usercontrol1: system. Windows. Forms. usercontrol
To facilitate the test, I added a textbox on it and registered the textchanged event of the textbox,
This. textbox1.textchanged + = new system. eventhandler (this. textbox#textchanged );
Event processing functions,
Private void textbox1_textchanged (Object sender, system. eventargs E)
{
MessageBox. Show (this. textbox1.text );
}
This example shows that if the text box content in the control changes, the current text box content will be displayed with MessageBox.
The control is displayed as follows:
Add the following user control to the form. When we change the text of Textbox, we can see that a dialog box is displayed, which is very simple.
The following describes how to add properties to a widget.
A private variable is defined here.
Private string customvalue;
Add access attributes
Public String customvalue
{
Get {return customvalue ;}
Set {customvalue = value ;}
}
When used in a form, it is accessed like a common control,
Usercontrol11.customvalue = "User Control Custom Data ";
The event can be used to transmit messages to the form. Before the definition, we should first write a simple parameter class.
Public class textchangeeventargs: eventargs
{
Private string message;
Public textchangeeventargs (string message)
{
This. Message = message;
}
Public String message
{
Get {return message ;}
}
}
Define the delegate,
Public Delegate void textboxchangedhandle (Object sender, textchangeeventargs E );
Next, add an event to the user control,
// Define the event
Public event textboxchangedhandle usercontrolvaluechanged;
To stimulate new events of user controls, modifyCode,
Private void textbox1_textchanged (Object sender, system. eventargs E)
{
If (usercontrolvaluechanged! = NULL)
Usercontrolvaluechanged (this, new textchangeeventargs (this. textbox1.text ));
}
Okay. In order to answer questions on csdn, paste 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 {return customvalue ;}
Set {customvalue = value ;}
}
// Define the event
Public event textboxchangedhandle usercontrolvaluechanged;
Public usercontrol1 ()
{
Initializecomponent ();
}
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Code generated by the region component designer
Private void initializecomponent ()
{
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. textbox#textchanged );
This. Controls. Add (this. textbox1 );
This. Name = "usercontrol1 ";
This. size = new system. Drawing. Size (150, 92 );
This. resumelayout (false );
}
# Endregion
Private void textbox1_textchanged (Object sender, system. eventargs E)
{
If (usercontrolvaluechanged! = NULL)
Usercontrolvaluechanged (this, new textchangeeventargs (this. textbox1.text ));
}
}
// Define the delegate
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 {return message ;}
}
}
}
Register the above events in the form when using them.Source codeNow,
Using 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 ()
{
Initializecomponent ();
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 code generated by Windows Form Designer
Private void initializecomponent ()
{
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 );
}
# Endregion
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
Private void usercontrol11_usercontrolvaluechanged (Object sender, textchangeeventargs E)
{
MessageBox. Show ("Current Control Value:" + E. Message );
}
}
}
In addition, you need to add the control to the controls set of the container. The following describes how to add the control to the constructor,
Public form1 ()
{
Initializecomponent ();
Usercontrol1 UC = new usercontrol1 ();
UC. customvalue = "Dynamically Loaded User Controls ";
UC. usercontrolvaluechanged + = new textboxchangedhandle (usercontrol11_usercontrolvaluechanged );
This. Controls. Add (UC );
}
In addition, drag the user control from the toolbox in vs.net to the form. If this is the first time you need to compile the project.
ArticleFinishing up: Western Digital-professional domain name registration and virtual hosting services
Http://www.west263.com
The above information is an integral part of the text. If you want to reprint this article, please keep the above information. Thank you!