新微赢技术网
标题: 如何用C#写一个简单的Login窗口发表日期 [打印本页]
作者: ★真的爱你★ 时间: 2009-3-16 22:44
标题: 如何用C#写一个简单的Login窗口发表日期
最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到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" )
uiLogin.UserName = txtUserName.Text;
uiLogin.Password = txtPassword.Text;
// Set dialog result with OK
this.DialogResult = DialogResult.OK;
// Wrong username or password
if( nLoginCount == MAX_LOGIN_COUNT )
this.DialogResult = DialogResult.Cancel;
MessageBox.Show( "Invalid user name and password!" );
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 )
除此外,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
// Set login info to class member
调用的时候,要修改程序的Main函数,如下: /// The main entry point for the application.
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!" );
MessageBox.Show( "Failed to logged in!" );
而附加的UserInfo类如下: private string strUserName;
private string strPassword;
get{ return strUserName;}
set{ strUserName = value; }
get{ return strPassword;}
set{ strPassword = value;}
作者: 朗月星空 时间: 2009-12-22 08:05
一个个全都骑到老大头上来了... 我也来骑一下。
作者: 孔隆 时间: 2010-2-5 17:05
我来自火星刚到地球什么都不懂
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) |
Powered by Discuz! X3.2 |