0

I set my LabelText invisible in Form1 and I have this button in Form2. Once if I cliked the button in Form2, the LabelText in Form1 will be visible. However, I'm trying to figure it out and it is still not working.

In Form1:

public void LabelText()
{
   label1.Visible = true;
}

In Form2:

Form1 frm1 = new Form1();
frm1.LabelText();
FoldFence
  • 2,674
  • 4
  • 33
  • 57

1 Answers1

2

You are creating new instance of Form1 instead of using existing you have previously displayed.

You can either 1. Use static class to keep all the handles or 2. Pass the Form1 instance in Form2 ctor

i.e.

1.

internal static class FormManager
{
    public static Form1 Form1Handle;
    public static Form2 Form2Handle;
}

and then in constructor

public Form1()
{
   FormManager.Form1Handle = this;
}

and for Form2 accordingly or

2. Override Form2 ctor

private Form1 _form1;

public Form2(Form1 form1Handle)
{
   _form1 = form1Handle;
}

and then call Form2 from Form1 like this:

Form2 f2 = new Form2(this);
f2.ShowDialog();

And then u use it depending on case:

  1. FormManager.LabelText();
  2. _form1.LabelText();
Peuczynski
  • 4,591
  • 1
  • 19
  • 33