For a long time, I have recently taken out, I have not been made in the application, it is a bit of a bit! I hope that this thing can help you. Everyone discusses common progress, this article is relatively simple! Yes As a universal login box control!
First build a resolution project logincontrol, add a class library project LoginTrol, then add user control CTLOGIN
Then write code in ctllogin.cs file
//ctllogin.cs
Using system; using system.componentmodel; using system.data; using system.windows.form; using system.text;
Namespace logincontrol {///
/// CTLLogin's summary description. ///
Public class ctllogin: System.windows.Forms.userControl
{
Private system.windows.Forms.Panel Panel1;
Private system.windows.forms.label Lheader;
Private system.windows.forms.label ltext;
Private system.windows.Forms.Button Btnlogin
Private system.windows.forms.label lpassword;
Private system.windows.forms.label lusername;
Private system.windows.Forms.TextBox txtpassword;
Private system.windows.Forms.TextBox txtusername;
Private system.windows.Forms.PictureBox PictureBox1;
Private system.windows.Forms.ErrorProvider ErrorProvider;
///
/// The required designer variable. ///
Private system.componentmodel.Container Components = NULL;
Public Event System.EventHandler Successful; Public Event System.EventHandler Failed;
Private arraylist (); private arraylist strpassword = new arraylist (); private int index = -1;
Private string errmsg = "Please enter the correct username and password!";
Public CTLLogin () {// This call is required for the Windows.Forms Form Designer. InitializationComponent ();
// Todo: Add any initialization strusename.clear () after the initializationComponent call; strpassword.clear ();
///
/// Clean all the resources being used. ///
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#REGION component designer generated code ///
/// Designer supports the required method - Do not use the code editor // / modify the contents of this method. ///
Private vidinitiRizeComponent () {
System.Resources.ResourceManager Resources = New System.Resources.ResourceManager (Typeof));
This.Panel1 = new system.windows.Forms.Panel ();
This.PictureBox1 = new system.windows.Forms.PictureBox ();
THIS.LTEXT = New System.windows.Forms.Label ();
This.lheader = new system.windows.forms.label ();
This.btnlogin = new system.windows.Forms.Button ();
This.lpassword = new system.windows.forms.label ();
This.lusername = new system.windows.forms.label ();
This.txtPassword = new system.windows.Forms.TextBox ();
THIS.TXTUSERNAME = new system.windows.Forms.TextBox ();
THIS.ErrorProvider = new system.windows.Forms.ErrorProvider ();
This.Panel1.suspendlayout ();
THIS.SUSPENDLAYOUT ();
//
// panel1
//
THIS.PANEL1.BACKCOLOR = system.drawing.systemcolors.activecaption;
This.Panel1.controls.add (this.picturebox1);
This.Panel1.controls.add (this.lText);
this.Panel1.controls.add (this.lheader);
This.Panel1.Location = new system.drawing.point (4, 4);
this.Panel1.name = "panel1";
This.Panel1.size = new system.drawing.size (480, 52);
this.Panel1.tabindex = 0;
//
// PictureBox1
//
This.PictureBox1.image = ((System.drawing.Image) ("PictureBox1.image)))));
This.PictureBox1.Location = new system.drawing.point (432, 12);
This.PictureBox1.name = "PictureBox1";
This.Picturebox1.size = new system.drawing.size (40, 32);
this.PictureBox1.tabindex = 6;
This.PictureBox1.tabstop = false;
//
// LText
//
THIS.LTEXT.LOCATION = New System.drawing.Point (36, 24);
This.lText.name = "ltd";
This.lText.size = new system.drawing.size (360, 20);
THIS.LText.tabindex = 5; this.lText.text = "Please enter your username and password."
THIS.LText.TextAlign = system.drawing.contentalignment.middleleft;
//
// LHEADER
//
This.lheader.font = new system.drawing.font ("taroma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((System.byte) (0)));
This.lheader.location = new system.drawing.point (12, 4);
this.lheader.name = "lheader";
this.lheader.size = new system.drawing.size (384, 20);
this.lheader.tabindex = 4;
THIS.LHETER.TEXT = "Login";
THISLHEADER.TEXTALIGN = System.drawing.ContentAlignment.middleleft;
//
// btnlogin
//
This.btnlogin.flatstyle = system.windows.Forms.Flatstyle.Popup;
This.btnlogin.font = new system.drawing.font ("tahoma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((System.byte) (0)));
This.btnlogin.location = new system.drawing.point (376, 292);
this.btnlogin.name = "btnlogin";
This.btnlogin.size = new system.drawing.size (70, 20);
this.btnlogin.tabindex = 14;
This.btnlogin.text = "landing";
This.btnlogin.click = new system.eventhandler (this.btnlogin_click);
//
// lpassword
//
This.lpassword.font = new system.drawing.font ("tahoma", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((System.byte) (0)));
THIS.LPassword.location = new system.drawing.point (76, 252);
this.lpassword.name = "lpassword";
this.lpassword.size = new system.drawing.size (80, 20);
THIS.LPassword.tabindex = 16;
this.lpassword.text = "Password";
This.lpassword.textAlign = system.drawing.contentAlignment.middleright;
//
// lusername
//
This.lusername.font = new system.drawing.font ("Taoma", 8.25F, System.drawing.FontStyle.Bold, System.drawing.graphicsUnit.point, ((System.byte) (0)));
THIS.LUSERNAME.LOCATION = New System.drawing.Point (76, 212);
this.lusername.name = "lusername";
THIS.LUSERNAME.SIZE = New System.drawing.size (80, 20);
THIS.LUSERNAME.TABINDEX = 15;
this.lusername.text = "User Name";
THIS.LUSERNAME.TEXTALIGN = System.drawing.contentAlignment.middleright;
//
// txtpassword
//
THIS.TXTPASSWORD.BORDERSTYLE = System.Windows.Forms.Borderstyle.fixedsingle;
THIS.TXTPASSWORD.LOCATION = New System.drawing.Point (176, 252);
this.txtpassword.name = "txtpassword";
THIS.TXTPASSWORD.PASSWORDCHAR = '*';
This.txtPassword.size = new system.drawing.size (270, 21);
This.txtPassword.tabindex = 13;
THIS.TXTPASSWORD.TEXT = "";
THIS.TXTPASSWORD.KEYPRESS = new system.windows.forms.keypressEventhandler (this.txtpassword_keypress);
This.txtPassword.Leave = new system.Eventhandler (this.control_leave);
This.txtPassword.Enter = new system.eventhandler (this.control_enter);
//
// txtusername
//
THIS.TXTUSERNAME.BORDERSTYLE = System.Windows.Forms.Borderstyle.Fixedsingle;
THIS.TXTUSERNAME.LOCATION = New System.drawing.Point (176, 212);
THIS.TXTUSERNAME.NAME = "TXTUSERNAME";
THIS.TXTUSERNAME.SIZE = New System.drawing.size (270, 21);
THIS.TXTUSERNAME.TABINDEX = 12;
THIS.TXTUSERNAME.TEXT = "";
THIS.TXTUSERNAME.KEYPRESS = New System.Windows.Forms.KeypressEventHandler (this.txtusername_KeyPress);
THIS.TXTUSERNAME.LEAVE = New System.EventHandler (this.control_leave);
THIS.TXTUSERNAME.ETER = New System.EventHandler (this.control_enter); //
// ErrorProvider
//
THIS.ERRORPROVIDER.CONTAINERCONTROL = THIS;
//
// ctllogin
//
This.Controls.Add (this.btnlogin);
This.Controls.add (this.lpassword);
This.Controls.add (this.lusername);
This.Controls.add (this.txtpassword);
This.Controls.add (this.txtusername);
This.Controls.add (this.Panel1);
THIS.NAME = "ctllogin";
This.Size = new system.drawing.size (484, 348);
This.Load = new system.eventhandler (this.ctllogin_load);
this.Panel1.ResumeLayout (false);
This.ResumeLayout (false);
} #Endregion
Private void ctllogin_load (Object sender, system.eventargs e) {// strusename.add (txtusername.text.trim ()); // strpassword.add (txtpassword.text.trim ());}
Private void txtpassword_keypress (object sender, system.windows.windows e) {if (e.keychar == (char) 13) {btnlogin_click (sender, new evenetargs ());}}
private void btnLogin_Click (object sender, System.EventArgs e) {bool b = CheckPassword (Username, Password); if (b) {Successful (this, new System.EventArgs ());} else {Failed (this, new System. Eventargs ());}}
///
/// Determine if the username and password are correct ///
///
All users
///
All user passwords
///
Private Bool Checkpassword (ArrayList Aluser, ArrayList Alpwd)
{
String struser = "";
String strpwd = "";
IF (aluser.count> 0 && alppwd.count> 0)
{
For (int i = 0; i
{
Struse = aluser [i] .tostring ();
Strpwd = alpwd [i] .tostring (). Trim ();
IF (struse.trim () == txtusername.text.trim () && strpwd.trim () == txtpassword.text.trim ())
{
Index_username = i;
ErrorProvider.dispose ();
Return True;
}
IF (i == aluser.count-1) {
ErrorProvider.seterror (btnlogin, errmsg);
Return False;
}
}
}
Return False;
}
Private Void Control_Enter (Object Sender, System.EventArgs E) {Control CTL = (Control) Sender; CTL.BackColor = Color.lemonChiffon;
Private Void Control_leave (Object Sender, System.Eventargs E) {Control CTL = (Control) Sender; CTL.BackColor = Color.White;}
Private void txtusername_keypress (object sender, system.windows.Forms.keypressEventArgs e) {if (E.KEYCHAR == (char) 13) {txtpassword.focus ();}}
///
/// All users ///
Public ArrayList Username
{
get
{
Return strusername;
}
set
{
Strusername = value;
}
}
///
/// All user passwords ///
Public ArrayList Password
{
get
{
Return strpassword;
}
set
{
Strpassword = value;
}
}
///
/// In the user array username, the index /////
Public int index_username
{
get
{
Return Index;
}
set
{
Index = value;
}
}
///
/// Error message ///
Public String ErrorMessage
{
get
{
Return errmsg;
}
set
{
Errmsg = Value;
}
}
}
}
Next, you can compile into LoginControl.dll
Add a test item TEST to the project, then add / remove the logincontrol.dll you just compiled into the toolbox, add a test interface to FRMTEST, then drag the CTLLogin control to the FRMTEST page in the toolbox.
Then test the just's control in frmtest.cs
//frmtest.cs
Using system; using system.collections; using system.componentmodel; using system.windows.form; using system.data; using informationclass; // write a test class
Namespace Test {////
/// Form1 summary description. ///
Public class frmtest: System.Windows.Forms.form
{
Private logincontrol.ctllogin ctllogin;
///
/// The required designer variable. ///
Private system.componentmodel.Container Components = NULL;
Private users objUsers = new users (); // Instantiate custom login user collection class private user m_objuser = new user (); // instantiate custom login user class
Public frmTest () {// // Windows Form Designer Support for // InitializationComponent (); // // Todo: Add any constructor code after INITIALIZECOMPONENT calls //}
///
/// Clean all the resources being used. ///
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code ///
/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///
Private vidinitiRizeComponent ()
{
System.Resources.ResourceManager Resources = New System.Resources.ResourceManager (TypeOf (fmtest));
THIS.CTLLOGIN = New LoginControl.ctllogin ();
THIS.SUSPENDLAYOUT ();
//
// ctllogin
//
THIS.CTLOGIN.ErrorMessage = "Please enter the correct username and password!";
this.ctllogin.index_username = -1;
this.ctllogin.location = new system.drawing.point (0, 0);
THIS.CTLOGIN.NAME = "ctllogin";
This.ctllogin.size = new system.drawing.size (484, 348);
this.ctllogin.tabindex = 0;
THIS.CTLLOGIN.FAILED = New System.EventHandler (this.ctllogin_failed);
This.ctllogin.Load = new system.eventhandler (this.form1_load);
THIS.CTLLOGIN.SUCCESSFUL = New System.EventHandler (this.ctllogin_suCcessful);
//
// frmtest
//
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
THIS.CLIENTSIZE = New System.drawing.size (484, 345);
This.Controls.Add (this.ctllogin);
This.FormBorderstyle = system.windows.Forms.FormBorderstyle.FixedSingle;
This.icon = ((System.drawing.icon) ("$ this.icon")))
THIS.MAXIMIZEBOX = FALSE;
THIS.MINIMIZEBOX = FALSE;
this.name = "frmtest";
This.Text = "System login";
This.Load = New System.EventHandler (this.form1_load);
this.ResumeLayout (false);} #ENDREGION
///
/// The main entry point for the application. ///
[Stathread]
Static void main ()
{
Application.run (new frmtest ());
}
Private void ctllogin_failed (Object sender, system.eventargs e) {messagebox.show ("login failed! Please enter the correct username and password !!", "Information Tips", MessageBoxButtons.ok, MessageBoxicon.ERROR);}
private void ctlLogin_Successful (object sender, System.EventArgs e) {m_objUser = objUsers [ctlLogin.Index_Username]; MessageBox.Show ( "successful landing!", "prompt message", MessageBoxButtons.OK, MessageBoxIcon.Information);}
private void Form1_Load (object sender, System.EventArgs e) {objUsers = new ReadData () GetUsers ();. if (objUsers.Count> 0) {foreach (User objUser in objUsers) {ctlLogin.Username.Add (objUser.Name ); Ctllogin.password.add (objUser.password);}}}}}
Run the program
If the login failure will appear
If the landing is successful
In this way, a small landing control is made, I hope that this article can help if there is nothing to learn, if there is any lack of places, I hope you can correct it, thank you.