开发者

C# - Multiple Forms HELP!

So, ive got these 2 forms, One is a button that when you click it, it creates another button Visible. That part works fine, this is the problem part

On the other form, that popups up when you click the original button, When i c开发者_JS百科lick the CLOSE button on Form2, its suppost to make the button on Form1 go invisible, But i can only refrence the form with this...

Form1 Form1 = new Form1();

This creates a NEW Form, i want to use the EXSISTING OPEN ONE! - HOW DO I DO THIS!

-- EDIT ---

Someone said that this

Form2 form2 = new form2();
form2.ShowDialog();
this.button.Visible = false;

Would work, This is wrong, i don't need to open a NEW form2, i need to REFERENCE the already open FORM2


What you need to do is add an event handler to the FormClosing or FormClosed event, then do what you want in the event handler, like so:

Just like this:

    private void button1_Click(object sender, EventArgs e)
    {
      Form2 form = new Form2();
      form.FormClosing += new FormClosingEventHandler(form_FormClosing);
      button2.Visible = true;
      form.Show() //Or form.ShowDialog(), your choice
    }

    void form_FormClosing(object sender, FormClosingEventArgs e)
    {
      button2.Visible = false;
    }


All you have to do is the following:

Form2 form2 = new form2();
form2.ShowDialog();
this.button.Visible = false;


First, turn off the caps lock or get your finger off the shift key.

Second, you can create a variable of type Form1 in your second form, then pass a reference to your first form in the constructor of the second, i.e., in the event handler that launches Form2,

Form2 f2 = new Form2(this);

You'll now have a reference back to your first form.


You can either pass in a reference of Form1 when you Create Form2 and show it (possibly via constructor) or register for Form2's Closing event in Form1 and on that event set the button's visibility property.


When you create form2 from form1, you'll want to attach to the OnClose() event of form 2.

You'd do this the same as any other click event.

Then in your Form2_OnClose() you could make the button.Visible = false;


There are other ways to reference the form. A quick search of stack overflow shows a good start: Windows Forms, getting a property from parent form

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜