Assigning DialogResult won't always close the form

S

Stefan M?ller

Hi all,
I have a form that is shown modally under some circumstances,
non-modally otherwise.
Assigning the DialogResult in modal state will usually close the form,
UNLESS the user clicked the x-Button the last time the form was shown
non-modally.
Probably the behavior depends on the way forms get reused or not, so
here's a simple example showing my problem:

Form1 with 2 buttons, one "ShowDialog", the other one "Show":

private Form2 childFrm = null;
private Form2 getForm(Type frmType) {
if (childFrm == null) childFrm = new Form2();
return childFrm ;
}

private void bShowDlg_Click(object sender, System.EventArgs e) {
Form2 frm = getForm(typeof(Form2));
frm.ShowDialog();
}
private void bShow_Click(object sender, System.EventArgs e) {
Form2 frm = (Form2) getForm(typeof(Form2));
frm.Show();
}

Form 2 has 2 Buttons, "Ok", "Cancel". It stores its Modal-State in
"isModal" (by the way -- is there a way to ask a running form for its
state?)

bool isModal = false;
public new DialogResult ShowDialog() {
isModal = true;
return base.ShowDialog();
}
public new void Show() {
isModal = false;
base.Show();
}

private void bOk_Click(object sender, System.EventArgs e) {
if (isModal) this.DialogResult = DialogResult.OK;
else this.Hide();
}
bCancel does the same for DialogResult.Cancel.

Now, if you only use those buttons, everythings's fine. If you click
"Show" on Form1, then close Form2 by using its (x), next time you open
it modally, you'll not get it closed via the buttons. The only thing
that happens is that the first time you press ok/cancel the form's
state changes from modal to nonmodal as can be seen from the form's
close-button switching to (x). Also the Closing-Event is obviously
thrown. But the Forms never disappears.

Any suggestions?
 
A

Alex Feinman [MVP]

I think the problem is in you using Hide() to dismiss the form. I'm pretty
sure that the behavior is undefined when you have a modal and modeless
instance of the same from at the same time
 
S

Stefan M?ller

Thank you for replying, but I can't make anything of your answer.
In case I wasn't clear enough:
I'm not having several instances of a form running side by side: I'm
reusing the same instance, which I would like to show
modally/modelessly -- in turn, not at the same time. There's no
mistake in doing so, is there?
What happens exactly, when someone closes a form by its (x)-Button
(i.e."MinimizeBox" is set to true)? Always thought, this is equivalent
to
Hide(), but it obviously isn't.

Sabine Weber
 
A

Anthony Wong [MSFT]

Closing a form by the "x" button is smart-minimizing the form, it is not
equivalent to calling Form.Hide() and after smart-minimizing the form, you
can still see the form in the list of running programs.

I think you can try adding a frm.Hide() call before frm.ShowDialog() inside
bShowDlg_Click() of Form1 to ensure that the Form2 instance is hidden.

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top