0

I has 2 form which is Form1 and Form2. In Form1, I has a button which will open Form2 in button click. In Form2, it use to do some setting for the label text in Form1. When close the form2, the label text in Form1 will update base on the setting, but I can make the label text update. Below is the code for update the text for label in Form1. I'm hardcode the text to simulate the situation.

Form1

public void languageChange()
        {
            labelControl5.Text = "AAAAAA";
            labelControl5.Invalidate();
            labelControl5.Update();
            labelControl5.Refresh();
            Application.DoEvents();

        }

In form2, I has below code to fire languageChange function in Form1.

 private void innoLanguage_FormClosed(object sender, FormClosedEventArgs e)
    {
        Main_new main = new Main_new();
        main.languageChange();
    }

It has call the function in the Form1 when form2 is close but it not update the label text.

I feel I make a mistake but I can't figure out. How I can make this work, please help.

Bubble Bub
  • 651
  • 4
  • 12
  • 32
  • You are creating a new Form1 it is not the one that opened your Form2 and is not visible, try either passing the creating form in the constructor or assign ownership by assigning the owning form when you use show or showdialog. – Mark Hall May 25 '15 at 03:45

1 Answers1

1

First thing's first you don't need to invalidate a Label when you change its Text variable, it will automatically redraw the control.

Secondly when the Form2 closes you created a new Main_new, called languageChange() on it, but then didn't actually show or display the form. I'm not sure what the situation is but if there's already a Main_new form open you don't need to make a new one, simply get the parent of the Form2 (which will be the Main_new already open), cast it as a Main_new, then call languageChange() on that.

Example

private void innoLanguage_FormClosed(object sender, FormClosedEventArgs e)
{
    ((Main_new)this.Parent).languageChange();
}

If Main_new isn't the name of the Form that opens Form2 change it to whatever does.

Prime
  • 2,410
  • 1
  • 20
  • 35
  • Hi AlphaDelta, the Main_New form is keep open while Form2 is open. I open the Form2 using Menu. When I close the form2, it show me an error "object not set an instance" in Form1 menu click. Below is the code I open Form2, new innoLanguage().ShowDialog(); What the error? – Bubble Bub May 25 '15 at 04:02
  • Is `Menu` the name of the form that opens `innoLanguage`? – Prime May 25 '15 at 04:04
  • Menu is the MenuStrip. The form name is Main_New private void languageSettingToolStripMenuItem_Click(object sender, EventArgs e) { new innoLanguage().ShowDialog(); } – Bubble Bub May 25 '15 at 04:32
  • 1
    Parent will be empty unless you assign it in the ShowDialog method. i.e. new innoLanguage().ShowDialog(this); – Mark Hall May 25 '15 at 04:38
  • Mark Hall....thanks for the guide. I should not make this mistake. Thanks. – Bubble Bub May 25 '15 at 06:51