这里的方法都涉及到了多窗体传递数据的问题,请参考我以前的一篇文章《多窗体传递数据》 http://www.cnblogs.com/voodooq/archive/2004/06/14/15446.aspx
1、Main窗体为登录窗体模式。
这种模式应该是比较方便而且简单的一种实现方法,
描述:
在Main窗体中建立登录信息等内容,然后点击登录按钮后在本窗体进行验证,当符合条件的时候,隐藏本窗体而显示功能窗体。
相关代码:
//
直截取了一段,用于判断的部分,大家可以看到在这里启动了应用。
应用程序的主入口点 #region 应用程序的主入口点
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new LogonFrm());
}
#endregion
Event #region Event
private void btnOk_Click(object sender, System.EventArgs e)
{
if(this.txtUserId.Text!=""&&this.txtPwd.Text!="")
{
this.User=this.txtUserId.Text.Trim();
this.sPwd=this.txtPwd.Text.Trim();
MainFrm main=new MainFrm(this.User,this.sPwd);//实例化窗体并传递参数
this.Visible=false;
main.Show();
}
else
{
MessageBox.Show("用户名和密码不能为空!");
this.txtUserId.Text="";
this.txtPwd.Text="";
this.txtUserId.Focus();
return;
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
应用程序的主入口点 #region 应用程序的主入口点
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new LogonFrm());
}
#endregion
Event #region Event
private void btnOk_Click(object sender, System.EventArgs e)
{
if(this.txtUserId.Text!=""&&this.txtPwd.Text!="")
{
this.User=this.txtUserId.Text.Trim();
this.sPwd=this.txtPwd.Text.Trim();
MainFrm main=new MainFrm(this.User,this.sPwd);//实例化窗体并传递参数
this.Visible=false;
main.Show();
}
else
{
MessageBox.Show("用户名和密码不能为空!");
this.txtUserId.Text="";
this.txtPwd.Text="";
this.txtUserId.Focus();
return;
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
2、弹出登录窗体模式
这种登录当时相对上面一种稍微复杂一点,需要使用多窗体数据传递的方法。
描述:
这里,将Main窗体作为登录后的处理窗体,而在Main窗体载入的时候,用ShowDialog()方法,显示登录窗体,然后在登录窗体进行相应的操作以及判断,并将数据传递过去。然后隐藏登录窗体,显示Main窗体。
代码:
//
截取一部分代码
// Main代码部分,在载入窗体的时候显示登录窗体
[STAThread]
static void Main()
{
LogonFrm lfrm = new LogonFrm();
MainFrm mfrm = new MainFrm();//将MainFrm实例化
lfrm.mfrm = mfrm;//对与登录窗体的对象
lfrm.ShowDialog();//显示登录窗体
Application.Run(mfrm);
}
// Main代码部分,在载入窗体的时候显示登录窗体
[STAThread]
static void Main()
{
LogonFrm lfrm = new LogonFrm();
MainFrm mfrm = new MainFrm();//将MainFrm实例化
lfrm.mfrm = mfrm;//对与登录窗体的对象
lfrm.ShowDialog();//显示登录窗体
Application.Run(mfrm);
}
//
这是登录窗体
public MainFrm mfrm;
// 将建立一个MainFrm类型的对象
// 处理登录操作
private void button1_Click( object sender, System.EventArgs e)
{
if(this.username.Text.Trim()!="" && this.pwd.Text.Trim()!="")
{
//将数据传递给MainFrm对象
mfrm.UserName = "用户名:"+this.username.Text;
mfrm.PWD = "密 码:"+this.pwd.Text;
this.Hide();
mfrm.Show();
}
else{
MessageBox.Show("用户名密码不能为空!");
}
}
public MainFrm mfrm;
// 将建立一个MainFrm类型的对象
// 处理登录操作
private void button1_Click( object sender, System.EventArgs e)
{
if(this.username.Text.Trim()!="" && this.pwd.Text.Trim()!="")
{
//将数据传递给MainFrm对象
mfrm.UserName = "用户名:"+this.username.Text;
mfrm.PWD = "密 码:"+this.pwd.Text;
this.Hide();
mfrm.Show();
}
else{
MessageBox.Show("用户名密码不能为空!");
}
}
3、使用独立进程的登录窗体
现在这种模式我还没有实现,概念就是登录窗体和Main窗体使用各自独立的进程,而当登录成功的时候不再是隐藏登录窗体,而是关闭。这样应该是对释放资源有一定好处的,呵呵
以上是我的一些个人看法,有什么问题,还请大家多多指正,或者您有更好的实现登录的方法,请告诉我。