Accessing variables in other Windows Form class [c

2020-02-06 10:47发布

问题:

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 7 years ago.

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!!

回答1:

So you have information in the parent form (form2) that you want to access in a method of the child form (form3).

  1. Create properties in form3 for the information that it will need.
  2. When form2 creates an instance of form3 it 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:

//TODO: give real name; adjust type as needed
public string SomePropertyName { get; set; }

then in form2 you can do:

f3.SomePropertyName = "hello from form2";

or

f3.SomePropertyName = someVariableInForm2;


回答2:

Man,

Try to create an overload of the constructor method of Form3, passing variable values ​​from form2 as method arguments.



回答3:

If you have made the variables in question public on Form2, then your issue is that you've also made them static. When you define them as static, you are placing them on the type (Form2) not on the instance (f2).

Remove the static from the variable declaration and they should appear in intellisense for f2.



回答4:

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?

Once you create instance of a class all the variables and methods declared as public should be available.Just recheck if you have declared your variables as public.

Since all the values are assigned during runtime, how could static variable do this?

No, Static variables and methods are defined with the start of the program.They dont need instances to be created to refer them.