I will appreciate if anyone can help me on this.
I have a windows form app that has three forms: form1, form2, form3. form1 starts when the app is activated. on form1, there is a button that brings up form2, and hide form1. there is also one button that brings up form3 and hides form2 on form2.
public partial class Form1 : Form
{
Form2 f2= new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
f2.Show();
}
}
public partial class Form2 : Form
{
Form3 f3 = new Form3();
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
f3.Show();
}
}
The question is on form3, i tried to access some of the variables that are assigned with values on runtime in form2. I think since i make f2 as modaless form, i should be able to access by simply using f2.myvariables, but the intellisense does not give me f2 object. Why is that? I found a way to declare those variables public static, so i could access by using form2.myvariables..Here is another thing that confuses me. Since all the values are assigned during runtime, how could static variable do this? I am a newbie on C#, and i already did a lot of searches on this, but seems no place answers my question exactly. Thanks for help in advance!!
So you have information in the parent form (form2) that you want to access in a method of the child form (form3).
form3for the information that it will need.form2creates an instance ofform3it should set those properties.You should think of this not as having the child form ask for information from it’s parent, but rather that the parent is giving information to its child. If you shift your mindset accordingly the code becomes not only easier to write, but also will be more in line with good coding practices (lower coupling, not exposing more information externally than needed, etc.)
To create a property you can do something like this in
form3:then in
form2you can do:or