Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 forms. In Form1 I have a label and in Form2 I have a button. When I press the button in Form2, I want the label in Form1 to change text to "new text".

How exactly should i write this?
Posted
Comments
[no name] 31-Mar-13 14:46pm    
Either pass the form to your other form or pass the label itself.
Member 9953050 31-Mar-13 14:50pm    
Well, it's just about the label, so I guess that passing the label itself would be best. But how is this done?
[no name] 31-Mar-13 15:02pm    
Just like passing any other kind of object.
Member 9953050 31-Mar-13 15:16pm    
Thanks. But it only works with the frm.Show();, and I don't want to open another window...
[no name] 31-Mar-13 15:21pm    
That makes no sense at all. You are already opening another window so why is it that you think you have to open yet another window? That is precisely why you are asked to post the code that you have tried for yourself, so we don't have to go back and forth trying to guess at what you are doing.

It's simple to achieve:
1) using form constructor
Form2 code:
C#
public partial class Form2 : Form
{
    //changed contructor:
    public Form2(string MyLabelText )
    {
        InitializeComponent();
        this.label1.Text = MyLabelText;
    }

}

Form1 code:
C#
private void button1_Click(object sender, EventArgs e)
{
    //
    Form2 frm = new Form2("new text for Label1");
    frm.Show();
}


2) using delegates
Using a delegate to pass data between two forms[^]
Delegates and Events[^]
Passing Data between Windows Forms[^]
 
Share this answer
 
Comments
[no name] 31-Mar-13 15:09pm    
I think you misunderstood. He wants to change the label text in form 1 from form 2. Not just pass a string to form 2. The label itself is in form 1.
Maciej Los 31-Mar-13 15:12pm    
Maybe... but second solution will do the job.
Thank you for your suggestion, ThePhantomUpvoter ;)
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
 
Share this answer
 
If the scenario is such that Form1 has a label and Form2 has a button that will change the label's text, then while you are loading Form2 you must make a custom constructor sending Form1's instant so that it can be manipulated.

For Example, inside Form1's constructor (I don't know how you will load Form2, I am loading it inside form1's constructor just to show you):

C#
public Form1()
{
     Form2 form2 = new Form2(this);
     form2.Show();
}


Then inside Form2 class make a public variable of Form1 type so it can be accessed by all methods. Make a custom constructor that takes in that value and assigns it to the global variable. Then manipulate that object in the button click method(assuming the label is called label1 in form1 and button is called button1 in form1):

C#
public class Form2
{
     public Form1 _form1;

     public Form2(Form1 form1)
     {
          _form1 = form1;
     }

     protected void button1_Click(object sender, EventArgs e)
     {
          //Assuming in Form1 label1 exists
          _form1.label1.Text = "New Text";
     }

}
 
Share this answer
 
v2

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