我有两个表单,登录表单和主表单。初始情况下,登录表单将被显示,当用户通过身份验证后,主表单将被显示,并关闭登录表单。
它基本上可以工作,但我必须点击btnLogin(登录表单中的按钮)两次才能关闭登录表单并显示主表单。
以下是我的代码。
Program.cs(登录表单)
namespace Login
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login fLogin = new Login();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new Main());
}
}
}
}
登录表单
namespace Login
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void Login_Load(object sender, EventArgs e)
{
}
private void btnLogin_Click(object sender, EventArgs e)
{
// initially btnLogin has a DialogResult property set to None
Authenticate();
}
private void Authenticate()
{
SqlCeConnection conn = new SqlCeConnection(@"Data source=d:/BIMS.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand(Properties.Resources.CheckIfUserExists, conn);
cmd.Parameters.Add("username", txtUsername.Text.Trim());
cmd.Parameters.Add("password", txtPassword.Text.Trim());
SqlCeDataReader dr = cmd.ExecuteReader();
bool hasRow = dr.Read();
if (hasRow)
{
btnLogin.DialogResult = DialogResult.OK;
}
}
}
}
你认为我哪里做错了吗?谢谢...