c#编写的简单登录(login)窗口

启动VC 2010
代码如下:
this.AcceptButton = this.btnOK;
this.CancelButton = this.btnCancel;

  private void btnOK_Click(object sender, System.EventArgs e)
  {
  // Here is to use fixed username and password
  // You can check username and password from DB
  if( txtUserName.Text == “Admin” && txtPassword.Text == “nopassword” )
  {
  // Save login user info
  uiLogin.UserName = txtUserName.Text;
  uiLogin.Password = txtPassword.Text;
  // Set dialog result with OK
  this.DialogResult = DialogResult.OK;
  }
  else
  {
  // Wrong username or password
  nLoginCount++;
if( nLoginCount == MAX_LOGIN_COUNT )
  // Over 3 times
  this.DialogResult = DialogResult.Cancel;
  else
  {
  MessageBox.Show( “Invalid user name and password!” );
  txtUserName.Focus();
  }
  }
  }
  private void btnCancel_Click(object sender, System.EventArgs e)
  {
  // Set dialog result with Cancel
  this.DialogResult = DialogResult.Cancel;
  }

  private void frmLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
  // Check whether form is closed with dialog result
  if( this.DialogResult != DialogResult.Cancel &&
  this.DialogResult != DialogResult.OK )
  e.Cancel = true;
  }

  private int nLoginCount = 0;
  private const int MAX_LOGIN_COUNT = 3;
  private UserInfo uiLogin;
  public frmLogin( ref UserInfo ui )
  {
  //
  // Required for Windows Form Designer support
//
  InitializeComponent();
  // Set login info to class member
  uiLogin = ui;
  }

  ///
  /// The main entry point for the application.
  ///
  [STAThread]
  static void Main()
  {
  UserInfo ui = new UserInfo();
  frmLogin myLogin = new frmLogin( ref ui );
  if( myLogin.ShowDialog() == DialogResult.OK )
  {
  //Open your main form here
  MessageBox.Show( “Logged in successfully!” );
  }
  else
  {
  MessageBox.Show( “Failed to logged in!” );
  }
  }

  ///
  /// User info class
  ///
  public class UserInfo
  {
  private string strUserName;
  private string strPassword;
  public string UserName
  {
  get{ return strUserName;}
  set{ strUserName = value; }
  }
  public string Password
  {
  get{ return strPassword;}
set{ strPassword = value;}
  }
  public UserInfo()
  {
  strUserName = ““;
  strPassword = ““;
  }
  }
图片截图省略

你可能感兴趣的:(c#编写的简单登录(login)窗口)