Reputation: 73
I have two forms, login and main, the main form is initially disabled (the groupBox
is disabled) this is code that runs when the user tries to login:
private async void btnLogin_Click(object sender, System.EventArgs e)
{
if (await VerifyUserNamePassword(txtUsername.Text, txtPassword.Text))
{
Frm_Main main = new Frm_Main();
main.Auth = true;
Close();
}
else
MessageBox.Show("Wrong username or password.");
}
and this is the Auth
property in the main form:
public bool Auth
{
set
{
if (value == true)
{
groupBox1.Enabled = true;
logOffToolStripMenuItem.Enabled = true;
loginToolStripMenuItem.Enabled = false;
listBoxUsers.DataSource = ctx.Users.ToList();
listBoxUsers.DisplayMember = "UserName";
listBoxUsers.ValueMember = "Id";
}
}
}
I run the debugger, it passes through all theses lines, it supposed to change the properties, but it doesn't. after the login form closed the main form still as it was, disabled? What am I missing?
Upvotes: 0
Views: 144
Reputation: 324
I think it is poor design to set in your login form the main.Auth.
The way i think is more appropriate is as follows:
In the loginForm have private boolean isLoginValid. Create a property for IsLoginValid. In the if statement you set the property IsLoginValid to true. Remove line to Frm_Main main = new Frm_Main();
In the main form you probably did something along the lines of login.ShowDialog() so after you process the login form, you assign within your main form main.Auth = Login.IsLoginValid
The reason this is preferred is that it separates your login and main forms logic. Please read about encapsulation and loosely-coupled applications
Upvotes: 1