C#Winform中的一个登录解说(转载的哟,比较不错)
2013-08-07 17:15
381 查看
最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到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函数,如下:
/// <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窗口,然后添加相应的输入框和按钮,设置窗口的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函数,如下:
/// <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 = "";
}
}
相关文章推荐
- 比较经典的为C# Winform程序设置登录窗体先启动,成功后再启动主窗口。
- C#WinForm应用程序实现自动填充网页上的用户名和密码并点击登录按钮【转载】
- [转载]C#开发Winform记录用户登录状态的方法
- C#的WINFORM登录窗体中,验证码的一个方法整理。
- 比较经典的为C# Winform程序设置登录窗体先启动,成功后再启动主窗口
- 比较经典的为C# Winform程序设置登录窗体先启动,成功后再启动主窗口
- C#的WINFORM登录窗体中,验证码的一个方法整理。
- c# 编写winform登录注册程序(二,注册部分)
- 一个学习数据库索引的比较详细和深入的教程,的确不错。
- Android应用程序开发教程:实现一个功能比较完善的登录对话框
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
- 转载:如何设计一个可扩展的用户登录系统
- C# Excel 行高,列宽,合并单元格,单元格边框线,冻结(转载) - 关于C#操作EXCLE常见操作比较全的 (防找不到)
- C# 一个特别不错的http请求类
- 【转载于百度空间】C# WinForm中的BackgroundWorker控件的使用方法
- C# 比较不错的通用验证码
- 【转载】C# winform在两个子窗体间传递参数
- C#下WinForm编程:登录窗体的设计
- C#解析XML各种方法特点比较(转载)
- C# WINFORM判断程序是否运行,且只能运行一个实例