I have three forms, f1, f2, f3 we'll call them. I have a class Account, in which I store username, password, groups and path (files). F2 is the main form, and should interact with both other forms. F1 is the login form and F3 is the edit form. I use this in my program.cs to call the login form before other forms.
Form1 login = new Form1();
if (login.ShowDialog() == DialogResult.OK)
{
Application.Run(new Form2());
}
To pass data between f2 and f3, I use a constructor with the account class. However, I would also like to pass data from f1 to f2, but if I use the same approach, I get no data interaction between the forms (data is null).
I edited the above code to this:
Form1 login = new Form1();
Account acc = new Account();
if (login.ShowDialog() == DialogResult.OK)
{
Application.Run(new Form2(acc));
}
In the login form (f1), I pass some data to the Account class, so upon login button click this happens:
private void LoginButton_Click(object sender, EventArgs e)
{
Account acc = new Account();
//some code
acc.Groups = "New group";
//some more code
DialogResult = DialogResult.OK;
}
Then, in the Form2.cs, I have the following code:
string groups = "";
public Form2(Account acc)
{
InitializeComponent();
groups = acc.Groups;
}
But whenever I run it like that, I never get any results in the main form (f2), acc.Groups is always null, and I do not know why exactly. I used the same approach with constructors to pull data from f2 to f3, and it worked fine, why doesn't this work? Is it because I use the wrong approach with the login form? What is the correct way to handle this?