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?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116
  • 4
    Why doesn't it work? What happens? – SLaks May 22 '12 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 '12 at 14:27

13 Answers13

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;
Davio
  • 4,609
  • 2
  • 31
  • 58
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);
Eric Dahlvang
  • 8,252
  • 4
  • 29
  • 50
6

Or you can do this >>

((Label)frm2.Controls["labelX1"]).Text = "test";
banging
  • 2,540
  • 1
  • 22
  • 26
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);
General Grey
  • 3,598
  • 2
  • 25
  • 32
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.

hawk
  • 1,827
  • 2
  • 14
  • 28
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

marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37
Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66
1
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
Hstm91
  • 11
  • 2
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
0

I changed my parent window property to the following code:

this.MdiParent.Controls["label1"].Text = "test";
maxshuty
  • 9,708
  • 13
  • 64
  • 77
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;
        }
    }
Joe Ruder
  • 2,122
  • 2
  • 23
  • 52
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.

Yuresh Karunanayake
  • 519
  • 1
  • 4
  • 10
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;
}
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
-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.

Val Bakhtin
  • 1,434
  • 9
  • 11