I have a login form and MDI main form in my c# windows application. In that I'm opening my login form like this in the MDI form load event. And when login is successful only it exits and enables MDI main form. Recently only I found out that if I close my login form it closes, and then it enables my MDI main without any barrier.
This is how I load my login in MDI main form.
private void MDiMain_Load(object sender, EventArgs e)
{
setDisplaysize();
Form newLogin = new FormControllers.FrmLogin();
newLogin.StartPosition = FormStartPosition.CenterScreen;
//newLogin.Show(this);
newLogin.ShowDialog(this);
newLogin.Focus();
newLogin.TopMost = true;
newLogin.Activate();
}
Then I tried to change my application like this using this code segment
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormControllers.FrmLogin fLogin = new FormControllers.FrmLogin();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MDiMain());
}
else
{
Application.Exit();
}
}
Now form login opens but after successful login the MDI main form does not start. What I'm doing wrong here?
Furthermore this is my code for login button in the login form
private void btnLogin_Click(object sender, EventArgs e)
{
string txtPass = "";
string txttPassword = "";
string txtHoldStr = "";
String txtStringst1 = "";
char chrFstep ='c';
char chrSstep ='c';
int testInt = 0;
using (DataControllers.RIT_Allocation_Entities EntityModel = new DataControllers.RIT_Allocation_Entities())
{
try
{
userHeadModel = EntityModel.TBLU_USERHED.Where(x => x.USERHED_USERCODE == (txtUserName.Text.Trim())).FirstOrDefault();
txtPass = userHeadModel.USERHED_PASSWORD;
txttPassword = txtPassword.Text.Trim();
if (txtPass == txtHoldStr)
{
MessageBox.Show("Login Successful");
this.Close();
}
else
{
MessageBox.Show("Invalid username or password please try again");
txtPassword.Focus();
}
}
catch (Exception ex) { }
}
}
You need to set the dialog result:
Only default buttons do this for you automatically. When there is logic involved, you need to set it depending on the result of - in this case - the authentication.
Apart from that, I guess the comparison to txtHoldStr in original code is wrong. This variable is always empty. To check if the password from the textbox matches that in the data model, compare txtPass to txttPassword.