Note: a critical piece missing in your question is: which Form is the TreeView on; it's kind-of logical to assume it's on Form1, but to really answer your question, you need to be absolutely sure of that.
The "essence" of the question you ask here: "how can I exchange information between Forms," is one of the most commonly asked, and answered questions on CP Q&A.
There are several valid strategies, including injection of references into instances of Forms (one Form instance has a reference to another Form's instance); having one Form define, and raise Events, that another Form (that has access to the instance of the Form) subscribes to, and exposure of Public properties on one Form, so that: when you create an instance of it, another Form has a reference to the instance of it, and can, thus, access the public properties of the other Form instance.
In your code for Form2: you create a
new instance of Form1:
Form1 myForm = new Form1;
string myString = myForm.lblProductID.Text;
: it's "blank," empty, and the 'lblProductID Control on 'myForm is, by default (and should not be changed), defined as 'private: you can't access it.
Since your code for Form1
creates the instance of Form2, so Form1 can keep a reference to the instance of Form2 created:
private Form2 InstanceOfForm2;
private void Form1_Load(object sender, EventArgs e)
{
InstanceOfForm2 = new Form2();
}
But, how does your instance of Form1 cause the instance of Form2 to have a Control to have some value set based on the Label on Form1: Well, one way is to create a public property on Form2 that can allow Form1 to set it, and to cause the setting of that property to change what is presented on Form2.
For example, suppose you put a TextBox 'textBox1 on Form2, and you add this property to Form2's class definition:
public string Form2TextBox1Text
{
get { return textBox1.Text; }
set
{
textBox1.Text = value;
}
}
Now, in Form1, you can do the following:
private Form2 InstanceOfForm2;
private void Form1_Load(object sender, EventArgs e)
{
InstanceOfForm2 = new Form2();
InstanceOfForm2.Form2TextBox1Text = lblProductID;
}
Think about what's happened here: you have "exposed" only the Text property of 'textBox1 on the instance of Form2 for use on an instance of Form1. And, that's all you've done:
1. so, now, an instance of Form1 can read, and write to the Text property of Form2 'textBox1: at any time.
But, now, think about what you have
not done:
1. If Form2's 'textBox1 Text property is edited on Form2, by the user, and you wanted to cause some action to happen in the instance of Form1, if the text changed:
you have not enabled that.
So, if you want a change on something on Form2 to trigger something happening on Form1: you are going to need to learn how to use/implement Events, and CodeProject is full of good resources to help you learn how to implement Events, and "expose" them, so objects (Classes, Forms)
outside where the Events are defined can subscribe to them.
Another way to say that is to say: that public Events in instances of objects enable notification of "consumers" of (subscribers to) those Events through .NET's multi-cast Event facility (which is based on Delegates).
As mentioned, there are many other techniques: we could have "injected" a reference to the instance of Form1 into Form2 (bad idea). Or, could have given the instance of Form1 access not to just the 'Text property of Form2's 'textBox1, but the TextBox itself.
That second idea might be appropriate, because: if Form1 has access to the TextBox on Form2, it could subscribe to the TextChanged Event of the TextBox, and then you would not have to write your own Event in Form2.
I
deliberately did not use this second alternative in this case, because I hope to direct your attention to the great importance of your learning about Delegates, and Events.