Mohamed Mohamed - 1 year ago 67
C# Question

I can't change controls properties in my property setter?

I have two forms, login and main, the main form is initially disabled (the

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;
MessageBox.Show("Wrong username or password.");

and this is the
property in the main form:

public bool Auth
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?

Answer Source

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:

  1. 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();

  2. 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download