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?
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?