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

Reading number is top 10 articles
visual c++中设备环境类CDC
使用php的zlib压缩和解压缩swf文件_[PHP教程]
Sql,server2000修复实践的一次说明_[SQL,Server教程]
SQL循序渐进(5)-------插入数据到表_[SQL,Server教程]
PHP高亮显示,XML,源代码_php资料_编程技术
javascript方法和技巧大全_JavaScript技术_编程技术
修改后的prototype.js,增加进度条和验证输入框_JavaScript技术_编程技术
Discuz!NT控件剖析,之,DataGrid(数据列表),[原创-,附源码]_[Asp.Net教程]
在asp.net2.0页面部分缓存中使用PartialCachingAttribu
ASP.NET状态管理,APPlication,Session,Cookie和ViewStat用法_[Asp.Net教程]
Reading number is top 10 pictures
The world first motorcycle will be auctioned for 21000 pounds
BingBingFan apple dew point photo gallery4
Earthquake hedge common sense
Plump breasts1
泳装名模阿尔贝特妮写真2
赵惟依写真2
Chinese paper-cut grilles art appreciation5
I also want to live a June 1 children's day, dad
The dog buy the ham oneself
BingBingFan apple dew point photo gallery3
Download software ranking
变速齿轮3.26
Tram sex maniac 2 (H) rar bag18
C#高级编程(第4版)
Sora aoi - one of more PK
Ashlynn Video4
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
The hero
徐若瑄成名作“魔鬼天使”
Unix video tutorial9
Macromedia Dreamweaver 8
aaa published in(发表于) 2013/12/18 8:11:19 Edit(编辑)
如何用C#写一个简单的Login窗口_.net资料_编程技术

如何用C#写一个简单的Login窗口_.net资料_编程技术

如何用C#写一个简单的Login窗口_.net资料_编程技术-你的首页-uuhomepage.com

  最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到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.