dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] how to change the text value of a label from another form ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Resolved [RESOLVED] how to change the text value of a label from another form ?

    hi All,

    how can i change the value of the label that is in Form1 by clicking the button in the Form2 form ?

    code from my Form2
    Code:
    private void button1_Click(object sender,EventArgs e)
    {
    Form1 frm = new Form1();
    frm.NewNameLbl.Text = "Hello";
    }
    code from my Form1 form

    I made my label2 in public so that can be access in other form, as per I read in other post.
    Code:
    public Label NewNameLbl {get{return label2;}}
    but when i run the program it didn't change the value of label2 .

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: how to change the text value of a label from another form ?

    Presumably Form1 is the startup form and it created the Form2 instance that you wanted to do this from. Think about it. That Form1 instance already exists and is displayed on the screen. If your Form2 instance creates a new instance of Form1 and does something to it, why would you expect that to affect the Form1 instance that is already displayed? If you had a notebook and you wanted to write on it, would you go out an get a new notebook and write on that and expect the notebook you already had to be affected? Of course not, because that would be magic. The same applies here. You can't do something to one object and expect it to affect another object just because they happen to be the same type. That would be like me slapping myself in the face and you getting a red mark on your cheek.

    If you want to affect the Form1 instance that already exists then you have actually refer to that instance. There are a few ways you can do that.

    Firstly, you could create a singleton that works much like default instances in VB. Check out a C# implementation of my original VB code here.

    Secondly, you could have the Form1 instance that creates the Form2 instance pass into that Form1 instance a reference to itself. That would be done via the constructor and Form1 would pass Me. Form2 can then assign that reference to a field of type Form1 and use that to affect the calling form.

    Using either of these two options, you should have a method in Form1 that Form2 can call and pass the new text as an argument. Form1 would then update its own Label in that method.

    The third option feels the most complex if you're not used to doing things the proper way but it is the proper way. The proper way is that Form2 has no idea that Form1 even exists and never affects it directly. Form2 would raise an event when it wants something to happen and make the required data available either via the event args for that event or a public property. When Form1 creates the Form2 instance, it handles that event. In the event handler, Form1 gets the text provided by Form2 and then updates its own Label.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: how to change the text value of a label from another form ?

    I got it.

    here is my form2, i declared string in my form2 called UserName

    Code:
       public form2(string UserName )
            {
                InitializeComponent();
                labelLogin.Text = UserName;
    
            }

    here is my form1 name Login form , I called form2 renamed it as Main
    then in the bracket i passed the string value textBoxUser.text
    Code:
      form2 Main = new form2(textBoxUser.Text);
                    Main.Show();
    this will passed the value of UserName from login form to form2 . saying Welcome : UserName

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: how to change the text value of a label from another form ?

    Quote Originally Posted by BONITO View Post
    I got it.

    here is my form2, i declared string in my form2 called UserName

    Code:
       public form2(string UserName )
            {
                InitializeComponent();
                labelLogin.Text = UserName;
    
            }

    here is my form1 name Login form , I called form2 renamed it as Main
    then in the bracket i passed the string value textBoxUser.text
    Code:
      form2 Main = new form2(textBoxUser.Text);
                    Main.Show();
    this will passed the value of UserName from login form to form2 . saying Welcome : UserName
    If that does what you need then great but it doesn't actually answer the question you asked in post #1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width