All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Delphi其他查询
初学者来看:了解什么是PHP和PHP的功能_php资料_编程技术
Cookie跨域、虚拟目录,,实现通行证登录_[Asp.Net教程]
GDI+绘制饼图分析男女比例
C#中定义鼠标指针为指定的动画图标实例
ASP.NET2.0数据库入门之SqlDataSource_[Asp.Net教程]
[delphi语法1]Object Pascal语言编写环境
一个功能齐全的DataGrid分页例子_[Asp.Net教程]
自动填充固定行数的,GridView_[Asp.Net教程]
让IIS支持PHP操作过程_php资料_编程技术
Reading number is top 10 pictures
南昌铁路局攸县车站125户铁路职工微利房终成骗局
Sora aoi on twitter5
传几朵花
From China fortress sora aoi1
The money of more than 100 countries and regions22
王艳写真温柔如水1
India's national beauty of the college students
2012 national geographic daily picture8
徐若瑄展示美丽胸围1
代沟,真好
Download software ranking
JSP+Ajax Web development typical examples
Unix video tutorial6
Photoshop 8.0图象编辑软件
c#程序设计案例教程
asp.netWeb服务器高级编程
Eclipse-CALMSANNY (second edition)
Proficient in Eclipse
apache-tomcat-6.0.33
The cock of the Grosvenor LTD handsome
Boxer vs Yellow2
delv published in(发表于) 2014/1/27 6:51:09 Edit(编辑)
如何用C#写一个简单的Login窗口_[Asp.Net教程]

如何用C#写一个简单的Login窗口_[Asp.Net教程]

如何用C#写一个简单的Login窗口_[Asp.Net教程]

  最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到Login窗口。其实,在Visual Studio 6以来,比较正确的做法是判断Login窗口的返回值,然后决定是否打开主窗体,那么在C#中也是一样的。


  具体做法如下:


  首先,创建Login窗口,然后添加相应的输入框和按钮,设置窗口的AcceptButton为窗体的确认按钮,而CancelButton为窗体的取消按钮。例如:


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;
  }


  然后,在Login窗体的Closing事件中,要进行处理,如下:


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;
  }


  除此外,Login窗体一些辅助代码如下:


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;
  }


  调用的时候,要修改程序的Main函数,如下:


///
  /// 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!" );
  }
  }


  而附加的UserInfo类如下:


///
  /// 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 = "";
  }
  }







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.