What's the problem in my code? c# winforms
I have two forms Login and Main form. Initially, the Login form will be displayed and when the user is authenticated, the Main form will be displayed and the Login form will be closed.
It's kinda working, but I have to click the btnLogin (a Button in the Login form) twice to close the Login form and show the Main form.
Here's my code.
Program.cs (Login form)
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());
}
}
}
}
Login form
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();
}
开发者_JS百科 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;
}
}
}
}
Where do you think I'm doing it wrong? Thanks....
just change
if (hasRow)
{
// btnLogin.DialogResult = DialogResult.OK;
this.DialogResult = DialogResult.OK;
this.close();
}
You should just call Form.Close()
in your button event handler, when you want to close the form (after setting desired DialogResult
).
As far as I know, it is not going to happen automagically.
I have never done it any other way, and this approach has always worked for me.
Try This:-
namespace Login
{
static class Program
{ /// /// The main entry point for the application. ///
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Login()); }
}
}
>
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)
{
Main formmain = new Main();
formmain.Show();
this.Dispose(); // U can also use this.Close();
}
}
}
}
It is a problem with your Authenticate() most likely. Can you remove that code and simply have
private void Authenticate()
{
btnLogin.DialogResult = DialogResult.OK;
}
This will tell you if if the problem is with the authentication code or with the gui.
If it's with your authen code it is possible that you need do a little more to access the database(check and see if you are connected, etc...)
As stated in the prev post, might be an issue with Authenticate method... May be it is taking a bit longer to access db.. Below code would help us know if the Authenticate has completed processing...
private void btnLogin_Click(object sender, EventArgs e)
{
btnLogin.Enabled = false;
// initially btnLogin has a DialogResult property set to None
Authenticate();
// better place call to Authenticate in try catch blocks
// to prevent btnLogin in a disabled state forever if Authenticate fails with
// an exception ...
// also if an exception occurs show that in a message box
btnLogin.Enabled = true;
}
And in Main, once the user has been authenticated, close the Login form
Login fLogin = new Login();
if (fLogin.ShowDialog() == DialogResult.OK)
{
fLogin.Close();
Application.Run(new Main());
}
精彩评论