我有两种形式的登录和主形式。最初,将显示登录表单,当用户通过身份验证时,将显示主表单并关闭登录表单。
它有点工作,但我必须单击 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;
}
}
}
}
你认为我哪里做错了? 谢谢....
最佳答案
只是改变
if (hasRow)
{
// btnLogin.DialogResult = DialogResult.OK;
this.DialogResult = DialogResult.OK;
this.close();
}
关于c# - 我的代码有什么问题?时间:2019-05-08 标签:c#winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4766524/