ShowDialog() while another form is visible through ShowDialog() as well, closes both?
I have the following situation:
ParentForm
which opensWelcomeF开发者_如何转开发orm
withShowDialog
.WelcomeForm
contains a Button which opensNewProjectForm
withShowDialog
- When the user hits
OK
onNewProjectForm
, a new project is created and both forms are closed (good behaviour) - When the user hits
CANCEL
onNewProjectForm
, both forms are closed (understandable behaviour, but not desireable).
I suspect this somehow has something to do with DialogResult
(which is actually DialogResult.None
on CANCEL
and NewProjectForm
)
How can I get the above situation without both forms closing?
Edit
Code used to openNewProjectForm
:
ProjectNew projectNew = new ProjectNew();
projectNew.StartPosition = FormStartPosition.CenterParent;
projectNew.ShowDialog(this);
Code used to open WelcomeForm
:
Welcome welcome = new Welcome();
welcome.StartPosition = FormStartPosition.CenterParent;
welcome.ShowDialog(this);
Note: NewProjectForm
is not opened by Welcome
but by ParentForm
Edit 2
Strange stuff; I created a new project with the same code/situation, which doesn't reproduce this behaviour...I got bitten by this because I had copy/pasted some buttons from another form, not realizing that one of the buttons had it's DialogResult property set to DialogResult.Cancel.
When clicked, this was setting the containing form's DialogResult, causing it to close.
Resetting it to DialogResult.None fixed the problem.
So, check the DialogResult properties on all your modal dialogs.
This has been a long-standing issue that I have run up against a few times. This post has more discussion on it, as well as a workaround.
It has apparently been fixed in .Net 3.5 SP1 (I can no longer reproduce it after installing SP1), although I haven't seen any official word on that it was definitely fixed.
Removed a couple of buttons, added a new one: solved.
So I think it should have been a DialogResult somehow, weird stuff.
精彩评论