20

I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried

form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;

But it does not work. Is there an easy, straight forward way of doing this?

2
  • 4
    Why doesn't it work? What happens?
    – SLaks
    May 22, 2012 at 14:25
  • 2
    Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
    – Steve
    May 22, 2012 at 14:27

13 Answers 13

33

You need to expose your label or its property.

In form 2:

public string LabelText
{
    get
    {
        return this.labelX1.Text;
    }
    set
    {
        this.labelX1.Text = value;
    }
}

Then you can do:

form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
9

You could modify the constructor of Form2 like this:

public Form2(string labelText)
{
    InitializeComponent();
    this.labelX1.Text = labelText;
}

then create Form2 passing in the text:

Form2 frm2 = new Form2(this.button1.text);
3
  • I agree this will work, but it would be a one time change. And maybe that is good enough for the OP. May 22, 2012 at 14:35
  • @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options. May 22, 2012 at 14:37
  • I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label. May 22, 2012 at 14:39
6

Or you can do this >>

((Label)frm2.Controls["labelX1"]).Text = "test";
5

inside form2 write this

public void ChangeLabel(string s)
{
    labelX1.Text = s;
}

then where you create Form 2 do this

form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
3

Is there an easy, straight forward way of doing this?

Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.

Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.

2
  • 1
    your answer is still not correct Visibility to protected will not help anything May 22, 2012 at 14:27
  • yea i accidentally wrote protected, i really meant public... i was editing while you commented!
    – hawk
    May 22, 2012 at 14:28
1
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
1

the only think you have to do is to put the label of the other form as public

for instance: Form1:

 public System.Windows.Forms.Label txtInfo;

then in Form2

Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
1

You can make labelX1 public, and it will work, but there is a better way to do this:

http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

0

I changed my parent window property to the following code:

this.MdiParent.Controls["label1"].Text = "test";
0

If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:

 public form2 form2_pub;

Then after you create it you assign the new one to your public instance:

form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2

Now you can reference form2_pub throughout your routines.

Works for me at least.

Remember, in your setter you can run whatever other code you want. For instance, I use the following to show what I want on another form by just setting show_scanning to true:

  public bool show_scanning //turns on the scanning screen
    {
        set
        {
            scanning_pnl.Visible = true;
            notReady_pnl.Visible = false;
            timer1.Enabled = true;
        }
    }
0

Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method, Go to your form Form2.Designer.cs

private System.Windows.Forms.Label labelX1;

Change 'private' into 'public'. Now the labelX1 is visible to outside.

0

The another approach is Just change the modifier property of label or text to public and now it allows to access the content of form1 textbox to label on another form

So the code is

private void button1_click(){
    Form2 obj1 =new Form2();
    Obj1.show();
    Obj1.label1.text="welcome"+textbox1.Text;
}
-1

Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.

3
  • @Killercam error is clear for who? He did not post any error information. May 22, 2012 at 14:30
  • If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
    – MoonKnight
    May 22, 2012 at 14:37
  • For some it is. You set access modifier from a prop box, not a code. But nvm. May 22, 2012 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.