Landing Box control

Source: Internet
Author: User
Tags bool count tostring trim
Control a long time ago to do things, recently took out to sneak, recently has not been the application of things, a little rusty! I hope this article can help you, we jointly explore common progress, this article is relatively simple! is to make a universal landing box control!

First build a solution project Logincontrol, add a project Logincontrol for a class library, and then add a user control Ctllogin



Then write the code in the CtlLogin.cs file

CtlLogin.cs

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;
Using System.Text;

Namespace Logincontrol
{
///
Summary description of the ctllogin.
///
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 strusername=new 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.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call
Strusername.clear ();
Strpassword.clear ();
}

///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region Component Designer
///
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
///
private void InitializeComponent ()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (Ctllogin));
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.ActiveCaptionText;
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) (resources. GetObject ("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 = "Ltext";
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 ("Tahoma", 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.lHeader.Text = "Landing";
This.lHeader.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 ("Tahoma", 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.Enter + = 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)
{
Strusername.add (TxtUsername.Text.Trim ());
Strpassword.add (TxtPassword.Text.Trim ());
}

private void Txtpassword_keypress (object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.keychar== (char) 13)
{
Btnlogin_click (Sender,new EventArgs ());
}
}

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 user name and password are correct
///
All Users
All user passwords
///
private bool Checkpassword (ArrayList aluser,arraylist alpwd)
{
String Struser= "";
String Strpwd= "";
if (aluser.count>0 && alpwd.count>0)
{
for (int i=0;i {
Struser=aluser[i]. ToString ();
Strpwd=alpwd[i]. ToString (). Trim ();
if (Struser.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;
}
}

///
The index in the user array username
///
public int Index_username
{
Get
{
return index;
}
Set
{
Index=value;
}
}

///
Error message
///
public string errormessage
{
Get
{
return errmsg;
}
Set
{
ErrMsg = value;
}
}
}
}




Then you can compile it into a LoginControl.dll

Add a test project to the solution and add/Remove items in the toolbox to add the LoginControl.dll you just compiled, add a test interface to frmtest, and then drag the Ctllogin control to the Frmtest page in the Toolbox

Then test the control in the FrmTest.cs

FrmTest.cs

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using Informationclass; A test class written by yourself

Namespace Test
{
///
Summary description of the Form1.
///
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 a custom login user collection Class
Private user M_objuser=new user (); Instantiate a custom login user class

Public Frmtest ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
private void InitializeComponent ()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (Frmtest));
This.ctllogin = new Logincontrol.ctllogin ();
This. SuspendLayout ();
//
Ctllogin
//
This.ctlLogin.ErrorMessage = "Please enter the correct username and password!";
This.ctlLogin.Index_Username =-1;
This.ctlLogin.Location = new System.Drawing.Point (0, 0);
This.ctlLogin.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) (resources). GetObject ("$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 ("Landing 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 ("Landing success!", "Information Tips", 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);
}
}
}
}
}



Running programs

If the landing fails, it will appear.





If the landing is successful





In this way, a small landing control is done well, I hope this article can be for just learning for a long time. NET programmer helps
If there is any shortage of places, I hope you can correct me, thank you





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.