views:

412

answers:

2

C# / .NET 3.5 / WinForms

I've got a form that opens a modal dialog form which opens another modal dialog form. The inner dialog form has OK and Cancel buttons and its AcceptButton and CancelButton are set to them respectively.

When I hit Enter or click OK in the inner dialog, the outer dialog closes as well. I can't see where I'm doing this - is this expected behaviour?

I can supply code but I didn't want to clutter this up.

A: 

The problem may be in the code that immediately follows the ShowDialog method call in the first dialog. Can you post that code?

Adam Ruth
+5  A: 

This happens because a ShowDialog call modifies its owners state as well.

To prevent this from happening, you need to reset the DialogResult of the first modal dialog to DialogResult.None after the ShowDialog call to the second dialog:

private void Button1_Click(object sender, EventArgs e)
{
    InnerDialog inner = new InnerDialog()
    DialogResult innerResult = inner.ShowDialog(this);
    this.DialogResult = DialogResult.None;
}

This has been a long-standing issue (see this post).

adrianbanks
That's it. I had a feeling I'd hit this one before a long time ago but I couldn't find it and Google didn't help. Problem solved. Thanks very much.
serialhobbyist
Maybe this is fixed? I have .Net 3.5 SP1 and do *NOT* see this behavior.
NascarEd
Possibly so. I've just tried it on .Net 3.5 SP1 and didn't get the behaviour. I've definitely had it on .Net 2.0 though.
adrianbanks