Click here to Skip to main content
15,311,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a situation where Form1 has a button to open Form2. In Form2, I need the value of one of the label fields (let's call it lblProductID). lblProductID gets populated when a user clicks on a TreeView item, and the TreeView itself gets populated from the results in a database.

I can access the label control (lblProductID) in Form1 from Form2, but the value is always blank. I have been digging for many hours on how to accomplish this seemingly simple task, and this is what I found.

In Form2 I have...

Form1 myForm = new Form1;
string myString = myForm.lblProductID.Text;

I debugged to look at the values and I noticed that all of the dynamically populated labels have blank values, even though I can clearly see them in Form1. I did notice that static label values are populated in debug mode.

So my question is, how do I get the dynamically populated value of a label on Form1 into Form2?

Thanks.
Posted
Comments
z3ngew 30-Aug-13 18:31pm
   
by populated you mean filled with text?
Motley Drew 30-Aug-13 22:16pm
   
Correct, filled with text.
idenizeni 30-Aug-13 19:37pm
   
Think of your form as an object... how would you pass data from one object to another?
Motley Drew 30-Aug-13 22:21pm
   
As a parameter. So could I do Form2.Show(myVar)? If so, my Form2 would need a parameter to recieve the object, so Form2 would be Public Form2(string myVar)? That makes some sense to me, I just didn't realize forms could accept parameters like that.
idenizeni 30-Aug-13 22:48pm
   
You wouldn't do it in the Show() method. You could make a property in the second form and set it before you call the Show() method. BillWoodruff posted a nice example a few minutes ago, check it out below.

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:
C#
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:
C#
public string Form2TextBox1Text
{
    get { return textBox1.Text; }
    set
    {
        textBox1.Text = value;
    }
}
Now, in Form1, you can do the following:
C#
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.
   
Comments
Boipelo 30-Aug-13 23:41pm
   
+5
I also like the way you explain, whether being the Launch or QA you always in detail and never assumes anything. You not only helping but also emphasis the importance of learning. Your style of writing is also tops - direct and clear.
BillWoodruff 31-Aug-13 2:30am
   
Thanks, Boipelo, feedback, like yours, means a lot to me !
Basic approach..
Passing Data Between Forms[^]

Better approach...
Passing Data between Windows Forms[^]
   
Comments
BillWoodruff 30-Aug-13 21:51pm
   
fyi: imho, the first article linked to presents very poor examples of techniques for inter-form communication. the second article, is mediocre, but does cover raising Events.
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
   
Well, issue is very simple. When you are using the following statements:

Form1 myForm = new Form1;
string myString = myForm.lblProductID.Text;

You are simply creating a new instance of Form1 and obviously the new instance will have the default values; that is why you are not getting any values other than the values of static members.

A very simple solution to your problem can be to pass values in the form of parameters from Form1 to Form2, which you need in Form2, when you open Form2 from the button of Form1.
OR
make those values static so that you can access them from Form2; but this is not a recommended approach. Best one is to pass the values in the form of parameters.
   
Comments
CHill60 31-Aug-13 18:55pm
   
How would the OP pass a parameter to a form? Do you mean "Properties"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900