Changing a label's text in another form in C#?












13















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?










share|improve this question




















  • 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






  • 1





    Does your code compile?

    – Francesco Baruchelli
    May 22 '12 at 14:30






  • 1





    Is that kind of joke question (puzzled by looking on your reputation)?

    – Val Bakhtin
    May 22 '12 at 14:34








  • 10





    Well, i am 15, and i am learning

    – Hunter Mitchell
    May 22 '12 at 14:44
















13















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?










share|improve this question




















  • 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






  • 1





    Does your code compile?

    – Francesco Baruchelli
    May 22 '12 at 14:30






  • 1





    Is that kind of joke question (puzzled by looking on your reputation)?

    – Val Bakhtin
    May 22 '12 at 14:34








  • 10





    Well, i am 15, and i am learning

    – Hunter Mitchell
    May 22 '12 at 14:44














13












13








13


4






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?










share|improve this question
















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?







c# winforms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 22 '12 at 14:25









SLaks

683k13916361756




683k13916361756










asked May 22 '12 at 14:23









Hunter MitchellHunter Mitchell

2,8121448102




2,8121448102








  • 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






  • 1





    Does your code compile?

    – Francesco Baruchelli
    May 22 '12 at 14:30






  • 1





    Is that kind of joke question (puzzled by looking on your reputation)?

    – Val Bakhtin
    May 22 '12 at 14:34








  • 10





    Well, i am 15, and i am learning

    – Hunter Mitchell
    May 22 '12 at 14:44














  • 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






  • 1





    Does your code compile?

    – Francesco Baruchelli
    May 22 '12 at 14:30






  • 1





    Is that kind of joke question (puzzled by looking on your reputation)?

    – Val Bakhtin
    May 22 '12 at 14:34








  • 10





    Well, i am 15, and i am learning

    – Hunter Mitchell
    May 22 '12 at 14:44








4




4





Why doesn't it work? What happens?

– SLaks
May 22 '12 at 14:25





Why doesn't it work? What happens?

– SLaks
May 22 '12 at 14:25




2




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





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




1




1





Does your code compile?

– Francesco Baruchelli
May 22 '12 at 14:30





Does your code compile?

– Francesco Baruchelli
May 22 '12 at 14:30




1




1





Is that kind of joke question (puzzled by looking on your reputation)?

– Val Bakhtin
May 22 '12 at 14:34







Is that kind of joke question (puzzled by looking on your reputation)?

– Val Bakhtin
May 22 '12 at 14:34






10




10





Well, i am 15, and i am learning

– Hunter Mitchell
May 22 '12 at 14:44





Well, i am 15, and i am learning

– Hunter Mitchell
May 22 '12 at 14:44












12 Answers
12






active

oldest

votes


















20














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;





share|improve this answer































    8














    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);





    share|improve this answer
























    • I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

      – General Grey
      May 22 '12 at 14:35











    • @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

      – Eric Dahlvang
      May 22 '12 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.

      – General Grey
      May 22 '12 at 14:39



















    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);





    share|improve this answer































      4














      Or you can do this >>



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





      share|improve this answer































        1














        You can me 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






        share|improve this answer































          1















          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.






          share|improve this answer





















          • 1





            your answer is still not correct Visibility to protected will not help anything

            – Micah Armantrout
            May 22 '12 at 14:27













          • yea i accidentally wrote protected, i really meant public... i was editing while you commented!

            – hawk
            May 22 '12 at 14:28






          • 1





            Or at least internal

            – Francesco Baruchelli
            May 22 '12 at 14:29



















          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





          share|improve this answer































            0














            I changed my parent window property to the following code:



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





            share|improve this answer

































              0














              form2 frm2 = new form2();
              ((Label)frm2.Controls["labelX1"]).Text=button1.Text;
              frm2.Show();





              share|improve this answer































                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;
                }
                }





                share|improve this answer































                  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.






                  share|improve this answer































                    -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.






                    share|improve this answer
























                    • Show him with an example. The error here is clear...

                      – MoonKnight
                      May 22 '12 at 14:28











                    • @Killercam error is clear for who? He did not post any error information.

                      – Val Bakhtin
                      May 22 '12 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 '12 at 14:37











                    • For some it is. You set access modifier from a prop box, not a code. But nvm.

                      – Val Bakhtin
                      May 22 '12 at 14:39











                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10704020%2fchanging-a-labels-text-in-another-form-in-c%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    12 Answers
                    12






                    active

                    oldest

                    votes








                    12 Answers
                    12






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    20














                    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;





                    share|improve this answer




























                      20














                      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;





                      share|improve this answer


























                        20












                        20








                        20







                        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;





                        share|improve this answer













                        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;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 22 '12 at 14:26









                        DavioDavio

                        3,11311644




                        3,11311644

























                            8














                            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);





                            share|improve this answer
























                            • I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

                              – General Grey
                              May 22 '12 at 14:35











                            • @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

                              – Eric Dahlvang
                              May 22 '12 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.

                              – General Grey
                              May 22 '12 at 14:39
















                            8














                            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);





                            share|improve this answer
























                            • I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

                              – General Grey
                              May 22 '12 at 14:35











                            • @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

                              – Eric Dahlvang
                              May 22 '12 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.

                              – General Grey
                              May 22 '12 at 14:39














                            8












                            8








                            8







                            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);





                            share|improve this answer













                            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);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 22 '12 at 14:31









                            Eric DahlvangEric Dahlvang

                            6,41342142




                            6,41342142













                            • I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

                              – General Grey
                              May 22 '12 at 14:35











                            • @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

                              – Eric Dahlvang
                              May 22 '12 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.

                              – General Grey
                              May 22 '12 at 14:39



















                            • I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

                              – General Grey
                              May 22 '12 at 14:35











                            • @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

                              – Eric Dahlvang
                              May 22 '12 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.

                              – General Grey
                              May 22 '12 at 14:39

















                            I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

                            – General Grey
                            May 22 '12 at 14:35





                            I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.

                            – General Grey
                            May 22 '12 at 14:35













                            @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

                            – Eric Dahlvang
                            May 22 '12 at 14:37





                            @K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.

                            – Eric Dahlvang
                            May 22 '12 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.

                            – General Grey
                            May 22 '12 at 14:39





                            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.

                            – General Grey
                            May 22 '12 at 14:39











                            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);





                            share|improve this answer




























                              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);





                              share|improve this answer


























                                5












                                5








                                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);





                                share|improve this answer













                                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);






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 22 '12 at 14:35









                                General GreyGeneral Grey

                                2,54521831




                                2,54521831























                                    4














                                    Or you can do this >>



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





                                    share|improve this answer




























                                      4














                                      Or you can do this >>



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





                                      share|improve this answer


























                                        4












                                        4








                                        4







                                        Or you can do this >>



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





                                        share|improve this answer













                                        Or you can do this >>



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






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered May 22 '12 at 14:36









                                        bangingbanging

                                        2,1691524




                                        2,1691524























                                            1














                                            You can me 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






                                            share|improve this answer




























                                              1














                                              You can me 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






                                              share|improve this answer


























                                                1












                                                1








                                                1







                                                You can me 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






                                                share|improve this answer













                                                You can me 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







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered May 22 '12 at 14:29









                                                Micah ArmantroutMicah Armantrout

                                                4,87632552




                                                4,87632552























                                                    1















                                                    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.






                                                    share|improve this answer





















                                                    • 1





                                                      your answer is still not correct Visibility to protected will not help anything

                                                      – Micah Armantrout
                                                      May 22 '12 at 14:27













                                                    • yea i accidentally wrote protected, i really meant public... i was editing while you commented!

                                                      – hawk
                                                      May 22 '12 at 14:28






                                                    • 1





                                                      Or at least internal

                                                      – Francesco Baruchelli
                                                      May 22 '12 at 14:29
















                                                    1















                                                    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.






                                                    share|improve this answer





















                                                    • 1





                                                      your answer is still not correct Visibility to protected will not help anything

                                                      – Micah Armantrout
                                                      May 22 '12 at 14:27













                                                    • yea i accidentally wrote protected, i really meant public... i was editing while you commented!

                                                      – hawk
                                                      May 22 '12 at 14:28






                                                    • 1





                                                      Or at least internal

                                                      – Francesco Baruchelli
                                                      May 22 '12 at 14:29














                                                    1












                                                    1








                                                    1








                                                    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.






                                                    share|improve this answer
















                                                    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.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited May 22 '12 at 14:31

























                                                    answered May 22 '12 at 14:25









                                                    hawkhawk

                                                    1,3931925




                                                    1,3931925








                                                    • 1





                                                      your answer is still not correct Visibility to protected will not help anything

                                                      – Micah Armantrout
                                                      May 22 '12 at 14:27













                                                    • yea i accidentally wrote protected, i really meant public... i was editing while you commented!

                                                      – hawk
                                                      May 22 '12 at 14:28






                                                    • 1





                                                      Or at least internal

                                                      – Francesco Baruchelli
                                                      May 22 '12 at 14:29














                                                    • 1





                                                      your answer is still not correct Visibility to protected will not help anything

                                                      – Micah Armantrout
                                                      May 22 '12 at 14:27













                                                    • yea i accidentally wrote protected, i really meant public... i was editing while you commented!

                                                      – hawk
                                                      May 22 '12 at 14:28






                                                    • 1





                                                      Or at least internal

                                                      – Francesco Baruchelli
                                                      May 22 '12 at 14:29








                                                    1




                                                    1





                                                    your answer is still not correct Visibility to protected will not help anything

                                                    – Micah Armantrout
                                                    May 22 '12 at 14:27







                                                    your answer is still not correct Visibility to protected will not help anything

                                                    – Micah Armantrout
                                                    May 22 '12 at 14:27















                                                    yea i accidentally wrote protected, i really meant public... i was editing while you commented!

                                                    – hawk
                                                    May 22 '12 at 14:28





                                                    yea i accidentally wrote protected, i really meant public... i was editing while you commented!

                                                    – hawk
                                                    May 22 '12 at 14:28




                                                    1




                                                    1





                                                    Or at least internal

                                                    – Francesco Baruchelli
                                                    May 22 '12 at 14:29





                                                    Or at least internal

                                                    – Francesco Baruchelli
                                                    May 22 '12 at 14:29











                                                    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





                                                    share|improve this answer




























                                                      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





                                                      share|improve this answer


























                                                        1












                                                        1








                                                        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





                                                        share|improve this answer













                                                        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






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Oct 31 '18 at 21:52









                                                        john pispidikisjohn pispidikis

                                                        111




                                                        111























                                                            0














                                                            I changed my parent window property to the following code:



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





                                                            share|improve this answer






























                                                              0














                                                              I changed my parent window property to the following code:



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





                                                              share|improve this answer




























                                                                0












                                                                0








                                                                0







                                                                I changed my parent window property to the following code:



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





                                                                share|improve this answer















                                                                I changed my parent window property to the following code:



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






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Oct 21 '15 at 14:18









                                                                maxshuty

                                                                1,83271838




                                                                1,83271838










                                                                answered Oct 21 '15 at 13:28









                                                                Amit GohelAmit Gohel

                                                                1




                                                                1























                                                                    0














                                                                    form2 frm2 = new form2();
                                                                    ((Label)frm2.Controls["labelX1"]).Text=button1.Text;
                                                                    frm2.Show();





                                                                    share|improve this answer




























                                                                      0














                                                                      form2 frm2 = new form2();
                                                                      ((Label)frm2.Controls["labelX1"]).Text=button1.Text;
                                                                      frm2.Show();





                                                                      share|improve this answer


























                                                                        0












                                                                        0








                                                                        0







                                                                        form2 frm2 = new form2();
                                                                        ((Label)frm2.Controls["labelX1"]).Text=button1.Text;
                                                                        frm2.Show();





                                                                        share|improve this answer













                                                                        form2 frm2 = new form2();
                                                                        ((Label)frm2.Controls["labelX1"]).Text=button1.Text;
                                                                        frm2.Show();






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Nov 10 '15 at 20:54









                                                                        Hstm91Hstm91

                                                                        11




                                                                        11























                                                                            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;
                                                                            }
                                                                            }





                                                                            share|improve this answer




























                                                                              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;
                                                                              }
                                                                              }





                                                                              share|improve this answer


























                                                                                0












                                                                                0








                                                                                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;
                                                                                }
                                                                                }





                                                                                share|improve this answer













                                                                                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;
                                                                                }
                                                                                }






                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Nov 1 '17 at 17:10









                                                                                Joe RuderJoe Ruder

                                                                                8711927




                                                                                8711927























                                                                                    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.






                                                                                    share|improve this answer




























                                                                                      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.






                                                                                      share|improve this answer


























                                                                                        0












                                                                                        0








                                                                                        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.






                                                                                        share|improve this answer













                                                                                        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.







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Nov 21 '18 at 4:18









                                                                                        Yuresh KarunanayakeYuresh Karunanayake

                                                                                        1006




                                                                                        1006























                                                                                            -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.






                                                                                            share|improve this answer
























                                                                                            • Show him with an example. The error here is clear...

                                                                                              – MoonKnight
                                                                                              May 22 '12 at 14:28











                                                                                            • @Killercam error is clear for who? He did not post any error information.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 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 '12 at 14:37











                                                                                            • For some it is. You set access modifier from a prop box, not a code. But nvm.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 at 14:39
















                                                                                            -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.






                                                                                            share|improve this answer
























                                                                                            • Show him with an example. The error here is clear...

                                                                                              – MoonKnight
                                                                                              May 22 '12 at 14:28











                                                                                            • @Killercam error is clear for who? He did not post any error information.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 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 '12 at 14:37











                                                                                            • For some it is. You set access modifier from a prop box, not a code. But nvm.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 at 14:39














                                                                                            -1












                                                                                            -1








                                                                                            -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.






                                                                                            share|improve this answer













                                                                                            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.







                                                                                            share|improve this answer












                                                                                            share|improve this answer



                                                                                            share|improve this answer










                                                                                            answered May 22 '12 at 14:26









                                                                                            Val BakhtinVal Bakhtin

                                                                                            1,264810




                                                                                            1,264810













                                                                                            • Show him with an example. The error here is clear...

                                                                                              – MoonKnight
                                                                                              May 22 '12 at 14:28











                                                                                            • @Killercam error is clear for who? He did not post any error information.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 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 '12 at 14:37











                                                                                            • For some it is. You set access modifier from a prop box, not a code. But nvm.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 at 14:39



















                                                                                            • Show him with an example. The error here is clear...

                                                                                              – MoonKnight
                                                                                              May 22 '12 at 14:28











                                                                                            • @Killercam error is clear for who? He did not post any error information.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 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 '12 at 14:37











                                                                                            • For some it is. You set access modifier from a prop box, not a code. But nvm.

                                                                                              – Val Bakhtin
                                                                                              May 22 '12 at 14:39

















                                                                                            Show him with an example. The error here is clear...

                                                                                            – MoonKnight
                                                                                            May 22 '12 at 14:28





                                                                                            Show him with an example. The error here is clear...

                                                                                            – MoonKnight
                                                                                            May 22 '12 at 14:28













                                                                                            @Killercam error is clear for who? He did not post any error information.

                                                                                            – Val Bakhtin
                                                                                            May 22 '12 at 14:30





                                                                                            @Killercam error is clear for who? He did not post any error information.

                                                                                            – Val Bakhtin
                                                                                            May 22 '12 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 '12 at 14:37





                                                                                            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 '12 at 14:37













                                                                                            For some it is. You set access modifier from a prop box, not a code. But nvm.

                                                                                            – Val Bakhtin
                                                                                            May 22 '12 at 14:39





                                                                                            For some it is. You set access modifier from a prop box, not a code. But nvm.

                                                                                            – Val Bakhtin
                                                                                            May 22 '12 at 14:39


















                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            Thanks for contributing an answer to Stack Overflow!


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function () {
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10704020%2fchanging-a-labels-text-in-another-form-in-c%23new-answer', 'question_page');
                                                                                            }
                                                                                            );

                                                                                            Post as a guest















                                                                                            Required, but never shown





















































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown

































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown







                                                                                            Popular posts from this blog

                                                                                            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                                                                            SQL update select statement

                                                                                            'app-layout' is not a known element: how to share Component with different Modules