Message transmission between user controls and forms in C #

Source: Internet
Author: User

If the events in the User-Defined control are hooked to the called Form Events in C #,

We all know that in Asp.net development, user-defined controls can effectively modularize programs. It can also be used in. Net winform.
Details: 1. Create an application windowsapplication1. 2. Add a new user control userlogin. (User) 3. Define User attributes
// Define attributes
Public String Username
{
Get {return username ;}
Set {username = value ;}
}
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}
4. Define Delegation
// Define the delegate
Public Delegate void btnokclickeventhander (Object sender, eventargs E );
Public Delegate void btncancelclickeventhander (Object sender, eventargs E );
5. Define events
// Define the event
Public event btnokclickeventhander btnokclick;
Public event btncancelclickeventhander btncancelclick
6. Event implementation
Private void textboxuid_textchanged (Object sender, system. eventargs E)
{
Username = This. textboxuid. text;
} Private void textboxpwd_textchanged (Object sender, system. eventargs E)
{
Password = This. textboxpwd. text;
} Private void buttonok_click (Object sender, system. eventargs E)
{
If (btnokclick! = NULL)
Btnokclick (this, e );
} Private void buttoncancel_click (Object sender, system. eventargs E)
{
If (btncancelclick! = NULL)
Btncancelclick (this, e );
}
7. Call user control events and receive messages in form1 winform.
Protected void okclick (Object send, system. eventargs E)
{
MessageBox. Show ("uid:" + userlogin1.username + "; PWD:" + userlogin1.password, "usermessage ");
} Protected void cancelclick (Object send, system. eventargs E)
{
This. Close ();
}
8. Press F5 to run (result) with 1 (windowsapplication1 source code)
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data; namespace windowsapplication1
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Panel Panel1;
Private windowsapplication1.userlogin userlogin1;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL; Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
Userlogin1.btnokclick + = new btnokclickeventhander (okclick );
Userlogin1.btncancelclick + = new btncancelclickeventhander (cancelclick );
//
// Todo: add Any constructor code after initializecomponent calls
//
} // <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
} # Code generated by region windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Panel1 = new system. Windows. Forms. Panel ();
This. userlogin1 = new windowsapplication1.userlogin ();
This. panel1.suspendlayout ();
This. suspendlayout ();
//
// Panel1
//
This. panel1.controls. Add (this. userlogin1 );
This.panel1.doc K = system.windows.forms.doc kstyle. Fill;
This. panel1.location = new system. Drawing. Point (0, 0 );
This. panel1.name = "Panel1 ";
This. panel1.size = new system. Drawing. Size (216,101 );
This. panel1.tabindex = 1;
//
// Userlogin1
//
This.userlogin1.doc K = system.windows.forms.doc kstyle. Fill;
This. userlogin1.location = new system. Drawing. Point (0, 0 );
This. userlogin1.name = "userlogin1 ";
This. userlogin1.size = new system. Drawing. Size (216,101 );
This. userlogin1.tabindex = 0;
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (216,101 );
This. Controls. Add (this. Panel1 );
This. Name = "form1 ";
This. Text = "form1 ";
This. panel1.resumelayout (false );
This. resumelayout (false );}
# Endregion // <summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
Protected void okclick (Object send, system. eventargs E)
{
MessageBox. Show ("uid:" + userlogin1.username + "; PWD:" + userlogin1.password, "usermessage ");
} Protected void cancelclick (Object send, system. eventargs E)
{
This. Close ();
}}
}
Appendix 2 (userlogin source code)
Using system;
Using system. collections;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. Windows. forms; namespace windowsapplication1
{
/// <Summary>
/// Summary of usercontrol1.
/// </Summary>
//// Define the delegate
Public Delegate void btnokclickeventhander (Object sender, eventargs E );
Public Delegate void btncancelclickeventhander (Object sender, eventargs E );
Public class userlogin: system. Windows. Forms. usercontrol
{
Private string username, password;
Private system. Windows. Forms. groupbox groupbox1;
Private system. Windows. Forms. Button buttoncancel;
Private system. Windows. Forms. Button buttonok;
Private system. Windows. Forms. textbox textboxpwd;
Private system. Windows. Forms. Label label2;
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. textbox textboxuid;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL; Public userlogin ()
{
// This call is required by the windows. Forms Form Designer.
Initializecomponent (); // todo: add any initialization after initializecomponent is called.} // <summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
} # Code generated by the region component designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. groupbox1 = new system. Windows. Forms. groupbox ();
This. buttoncancel = new system. Windows. Forms. Button ();
This. buttonok = new system. Windows. Forms. Button ();
This. textboxpwd = new system. Windows. Forms. Textbox ();
This. label2 = new system. Windows. Forms. Label ();
This. label1 = new system. Windows. Forms. Label ();
This. textboxuid = new system. Windows. Forms. Textbox ();
This. groupbox1.suspendlayout ();
This. suspendlayout ();
//
// Groupbox1
//
This. groupbox1.controls. Add (this. buttoncancel );
This. groupbox1.controls. Add (this. buttonok );
This. groupbox1.controls. Add (this. textboxpwd );
This. groupbox1.controls. Add (this. label2 );
This. groupbox1.controls. Add (this. label1 );
This. groupbox1.controls. Add (this. textboxuid );
This. groupbox1.location = new system. Drawing. Point (8, 2 );
This. groupbox1.name = "groupbox1 ";
This. groupbox1.size = new system. Drawing. Size (200, 96 );
This. groupbox1.tabindex = 6;
This. groupbox1.tabstop = false;
//
// Buttoncancel
//
This. buttoncancel. Location = new system. Drawing. Point (112, 71 );
This. buttoncancel. Name = "buttoncancel ";
This. buttoncancel. size = new system. Drawing. Size (56, 20 );
This. buttoncancel. tabindex = 11;
This. buttoncancel. Text = "& cancel ";
This. buttoncancel. Click + = new system. eventhandler (this. buttoncancel_click );
//
// Buttonok
//
This. buttonok. Location = new system. Drawing. Point (32, 71 );
This. buttonok. Name = "buttonok ";
This. buttonok. size = new system. Drawing. Size (56, 20 );
This. buttonok. tabindex = 10;
This. buttonok. Text = "& OK ";
This. buttonok. Click + = new system. eventhandler (this. buttonok_click );
//
// Textboxpwd
//
This. textboxpwd. Location = new system. Drawing. Point (72, 43 );
This. textboxpwd. Name = "textboxpwd ";
This. textboxpwd. passwordchar = *;
This. textboxpwd. size = new system. Drawing. Size (112, 21 );
This. textboxpwd. tabindex = 9;
This. textboxpwd. Text = "";
This. textboxpwd. textchanged + = new system. eventhandler (this. textboxpwd_textchanged );
//
// Label2
//
This. label2.location = new system. Drawing. Point (9, 42 );
This. label2.name = "label2 ";
This. label2.size = new system. Drawing. Size (64, 23 );
This. label2.tabindex = 8;
This. label2.text = "Password :";
This. label2.textalign = system. Drawing. contentalignment. middlecenter;
//
// Label1
//
This. label1.location = new system. Drawing. Point (9, 14 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (64, 23 );
This. label1.tabindex = 7;
This. label1.text = "username :";
This. label1.textalign = system. Drawing. contentalignment. middlecenter;
//
// Textboxuid
//
This. textboxuid. Location = new system. Drawing. Point (72, 15 );
This. textboxuid. Name = "textboxuid ";
This. textboxuid. size = new system. Drawing. Size (112, 21 );
This. textboxuid. tabindex = 6;
This. textboxuid. Text = "";
This. textboxuid. textchanged + = new system. eventhandler (this. textboxuid_textchanged );
//
// Userlogin
//
This. Controls. Add (this. groupbox1 );
This. Name = "userlogin ";
This. size = new system. Drawing. Size (216,104 );
This. groupbox1.resumelayout (false );
This. resumelayout (false );}
# Endregion
// Define attributes
Public String Username
{
Get {return username ;}
Set {username = value ;}
}
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}

// Define the event
Public event btnokclickeventhander btnokclick;
Public event btncancelclickeventhander btncancelclick;
Private void textboxuid_textchanged (Object sender, system. eventargs E)
{
Username = This. textboxuid. text;
} Private void textboxpwd_textchanged (Object sender, system. eventargs E)
{
Password = This. textboxpwd. text;
} Private void buttonok_click (Object sender, system. eventargs E)
{
If (btnokclick! = NULL)
Btnokclick (this, e );
} Private void buttoncancel_click (Object sender, system. eventargs E)
{
If (btncancelclick! = NULL)
Btncancelclick (this, e );
}

}
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.