winform登录窗口的正确操作办法!--源自http://blog.csdn.net/knight94/archive/2006/04/06/652394.aspx
2008-12-04 11:11
211 查看
本文原出处:http://blog.csdn.net/knight94/archive/2006/04/06/652394.aspx 稍作修正
最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到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();
this.DialogResult=DialogResult.none;
}
}
}
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函数,如下:
/// <summary>
/// The main entry point for the application.
/// </summary>
[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类如下:
/// <summary>
/// User info class
/// </summary>
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 = "";
}
}
最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到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();
this.DialogResult=DialogResult.none;
}
}
}
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函数,如下:
/// <summary>
/// The main entry point for the application.
/// </summary>
[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类如下:
/// <summary>
/// User info class
/// </summary>
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 = "";
}
}
相关文章推荐
- winform登录窗口的正确操作办法(转)
- winform登录窗口的正确操作办法
- winform登录窗口的正确操作办法
- 3月30日 winform如何正确合理关闭登录窗口
- 连接SQLServer时提示“但是在登录前的握手期间发生错误。 (provider: SSL Provider, error: 0 - 等待的操作过时”解决办法
- Winform下动态执行JavaScript脚本获取运行结果,谈谈网站的自动登录及资料获取操作
- C# winform登录窗口关闭方法
- 有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。这样,使用asphttp等组件就难以正确得到所要的信息。
- oracle 登录 http://localhost:5560/isqlplus/login.uix 任意账号无法登陆的解决办法
- oracle 登录 http://localhost:1158/em/console 不显示,账号无法登陆的解决办法
- Filezilla无法确定拖放操作目标,由于shell未正确安装__解决办法
- .net (c#)winform中操作ie窗口元素
- winform窗口中实现按Enter键动态将光标定位到下一个textBox控件。以登录窗口为例
- 如何在C#Winform程序中正确使用登录窗体
- 在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口
- C# winform登录窗口关闭方法
- 没有终结点在侦听可以接受消息的 http://******/Service.asmx。 这通常是由于不正确的地址或者 SOAP 操作导致的。如果存在此情况, 请参见 InnerExc
- Ubuntu启用root账户后,登录root账户时报错:读取/root/.profile时发现错误:mesg:ttyname failed:对设备不适当的ioctl操作 作为结果,会话不会被正确配置
- 浅谈如何在C#Winform程序中正确使用登录窗体
- [Winform]安装在C盘,无操作权限的一个解决办法