views:

572

answers:

2

Hello everyone,

I am using C# + VSTS2008 + .Net 2.0 to develop a Windows Forms application. In button1 event handler of Form1, I create Form2, then Form2 appears. Then when trigger event handler of button2 of Form2 (button2 is Form2's UI button element), after executing button2 event handler, Form2 will disappear. Here is part of my code which creates Form2. Any ideas what is wrong to cause Form2 disappear?

// button1 belongs to Form1
private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2("www.google.com");
    form2.ShowDialog();
}

thanks in advance, George

+2  A: 

Without more details it's impossible to say for sure, but if button2 in Form2 has a value assigned to the DialogResult property, this will cause the form to hide automatically when the button is clicked. Open Form2 in the designer, select the button and check in the property grid. If the DialogResult property is anything else than "None", this is expected behaviour.

From the MSDN documentation of the Button.DialogResult property:

If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.

Fredrik Mörk
The event handler of button2 is empty. Any ideas what is wrong?
George2
Read my answer again; it's not about the event handler, it's about the DialogResult property of button2. But if the event handler is empty, it strengthens my suspicion that it is the DialogResult property that causes the behaviour.
Fredrik Mörk
I have checked that the dialog result of button 2 is None, is that expected value?
George2
Sorry Fredrik, I find the root cause, I set button2 to be the CancelButton of Form2, does that cause the Form2 to disappear?
George2
Yes, as Colin suggests below, that may also cause the Form to disappear. You should mark that as the accepted answer ;o)
Fredrik Mörk
@George2 Setting the CancelButton property on the form will cause the form to disappear when the associated button is pressed.
Colin Mackay
@Colin: ha ha, with your comment there I almost come out as clairvoyant :o)
Fredrik Mörk
@Fredrik Mörk: Good catch on the possibility of setting the DialogResult. Just tried it and it dismissed the dialog. Never knew that. I always set it then closed the dialog myself.
Colin Mackay
@Fredrik Yes, I see what you mean. :)
Colin Mackay
+4  A: 

Form2 is being opened as a dialog. Could the button being pressed also be defined as the CancelButton (The CancelButton property will be on form2). Doing that will automatically dismiss the dialog when the event handlers have completed.

Colin Mackay
As an interesting side note, setting the AcceptButton will *not* automatically dismiss the form. It may seem inconsequent, but is logical I guess; if we cancel we usually just want to go away, while accepting may include some work before we leave, and may lead to us wanting the dialog to stay (if some validation fails for instance).
Fredrik Mörk