Home > Net >  Issue with changing Label's text of another form
Issue with changing Label's text of another form

Time:08-13

I have two forms Form1 and Form2, the Form1 contains a label called label1 (public) now that on Form2 contains a TextBox and a button that supposed to retrieves the input from the TextBox and I was trying to change the text of label1 from Form1 form to the input of the TextBox from Form2.

Here's what I've tried so far,

Form1 class:

 public partial class Form1 : Form
    {
        public Form1()
        {  
           InitializeComponent();
           this.Text = "Main Form";
         }


    public void label1_Click(object sender, EventArgs e) {
        
    }

       public void change_label_func(string ss) {
           this.label1.Text = ss;
       }
       
           public void button1_clicked(object sender, EventArgs e) {
                  Form4 sec_form = new Form4();
                  sec_form.Show();
}

 }

Form2 class:

public partial class Form4 : Form
    {
        public Form4() {
            InitializeComponent();
            this.Text = "Create New Directory Page";
            this.Icon = new Icon(@"C:\Users\USER\Documents\FlowStorage4.ico");

        }

        private void textBox2_TextChanged(object sender, EventArgs e) {

       }

        public void guna2Button2_Click(object sender, EventArgs e) {    
         
             Form1 get_dir = new Form1();
             get_dir.change_label_func(textBox2.Text);
       }

Now that when I clicked the button the label on Form1 makes no changes.

CodePudding user response:

Every form has a message loop inside which handles updates when the value changes, this has been like that since the beginning of windows.

So:

A window has a windowprocedure attached to it. A function where all messages pass through. The messages are sent to the function because there is a thread running in an endless loop. (until WM_CLOSE)

while (GetMessage(&msg, NULL, 0, 0))
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

When you say

label1.Text = "new";

several messages are sent to the windows message queue and they are handled in order. Eg one message that is sent is WM_PAINT which will handle the redrawing of the pixels that make up the text "new".

So sharing labels between forms is generally not done. The message queue handling of one window can be suspended while you set new text n the other. weird things start to happen.

CodePudding user response:

The problem is in your code. you are always using a new instance of Form1 by this line :

Form1 get_dir = new Form1();

You need to use the instance of Form1 from where you are opening Form2. To do this, declare a Form variable in your Form2 class like below :

Public Form parentFrm = new Form();

And when you are opening Form2 in your Form1, send the instance of Form1 like below :

Form4 sec_form = new Form4();
sec_form.parentFrm = this;

And then, use this instance in your guna2Button2_Click event like below :

public void guna2Button2_Click(object sender, EventArgs e) 
{    
    parentFrm.change_label_func(textBox2.Text);
}

I have not tested the code, but that's the one of the concepts to make it works.

  • Related