user350034
user350034

Reputation:

How can I update a label on one form from another form in C#?

I want to update label of Form1 from Form2. So here's what I have:

// Form1
public string Label1
{
    get { return this.label1.Text; }
    set { this.label1.Text = value; }
}


// Form2
private void button1_Click(object sender, EventArgs e)
{
    Form1 frm1 = new Form1();
    frm1.Label1 = this.textBox1.Text;
    this.Close();
}

So the above code doesn't work. However, when I add the following:

frm1.Show();

after

this.Close();

in Form2 code, the Form1 obviously is being opened again (two windows). But I want it to update in the same window so I suggest this.Close() is unnecessary.

Anyone have any ideas?

Upvotes: 0

Views: 9340

Answers (5)

ShahidAzim
ShahidAzim

Reputation: 1494

MVP pattern is good for this type of senario and also separate UI and business logic.

This thread is good reference: UI Design Pattern for Windows Forms (like MVVM for WPF)

Upvotes: 0

indiPy
indiPy

Reputation: 8072

When you say this.close that means all the values you set to that instance gets nullifies.

You can use delegate here, to set form values.

Upvotes: 0

Mohammed Thabet
Mohammed Thabet

Reputation: 21677

Try using singleton design pattern for that as I think the problem is when you using new

public class Form1Singleton {

    private static final Form1Singleton INSTANCE = null; 
    // Private constructor prevents instantiation from other classes
    private Form1Singleton () {
    }

    public static Form1Singleton getInstance() 
    {
       if(INSTANCE ==null)
            INSTANCE =new Form1Singleton();

        return INSTANCE;
    }

}

Now you can use the form easily only 1 form

private void button1_Click(object sender, EventArgs e) 
{     
Form1Singleton frm1 = Form1Singleton.getInstance();

frm1.Label1 = this.textBox1.Text;     
this.Close(); 
} 

I hope that solve your problem

BR,

Mohammed Thabet Zaky

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244981

In the button1_Click method, you're actually creating a new instance of Form1 and setting the Label1 property of that new instance. That explains why a second instance of Form1 is being shown on the screen when you add frm1.Show();. If you look at that second copy carefully, you'll see that it's label displays the correct value, but your original copy of that form retains its original value.

Instead, I assume that what you want to do is set the Label1 property for the existing instance of Form1 that is already displayed on the screen. In order to do that, you're going to have to get a reference to that existing form object.

Thus, this becomes a simple question of "how do I pass data between two objects (forms)", which has been asked countless times here on Stack Overflow. See the answers to these related questions:

Upvotes: 6

Ondrej Tucny
Ondrej Tucny

Reputation: 27974

Most probably: What you actually do is create a brand new instance of Form1 within the scope of Form2, while later in the control flow of your program another instance of Form1 is created and shown. Of course, in such a case you can't expect your label to be set correctly.

The best would be to decouple Form1 and Form2 from each other and instead pass only data along the control flow of your program.

Upvotes: 0

Related Questions