Reputation: 11576
I've got a MDI-Application in which I'd like to use modal dialogs...yes, I know that this is a little against the principles if MDI...anyway, my main window is more of a 'workspace' then anything else.
Back to topic, how can I wait for an MDI-Child to close? Some example code:
public void DoSomething() {
String searchterm = this.TextBox1.Text;
MyItem result = MySearchForm.GetItem(searchterm);
if(MyItem != MyItem.Empty) {
// do something
}
}
MySearchForm is a MDI-Child of the main window, so I can't use ShowDialog(), but I'd still like to use a blocking method to wait for the window to close and return the result. I thought about calling it on another thread and wait for that one to exit, but that also doesn't work with MDI.
Does somebody have an idea?
Upvotes: 2
Views: 4756
Reputation: 101140
Simply move focus back to the MDI child if it loses it. Hook the LostFocus event in the MDI child window and use this.SetFocus();
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter.aspx
Upvotes: 0
Reputation: 941337
Using dialogs in an MDI application is quite normal, it doesn't violate MDI conventions. Just don't make it a MDI child window. That's bad because you cannot make it modal. And if you make it non-modal then confuzzling things happen when the user minimizes the window.
Just use the ShowDialog(owner) or Show(owner) method (respectively modal and non-modal) and pass the MDI parent as the owner. The dialog will always be on top of the child windows. You typically do want StartPosition = Manual and set Location so you can be sure it starts up at an appropriate position within the parent frame.
Upvotes: 2
Reputation: 14021
Try disabling the main form, then re-enabling it when the child form closes. It would be a bit like this:
public void DoSomething()
{
searchForm.Show();
searchForm.SearchTerm = this.TextBox1.Text;
searchForm.FormClosing += new FormClosingEventHandler(searchForm_FormClosing);
this.Enabled = false
}
void searchForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Enabled = true;
// Get result from search form here
MyItem result = searchForm.GetItem();
if(MyItem != MyItem.Empty) // do something
}
Upvotes: 2
Reputation: 337
Overload the FormClosing event of the main window:
void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
// User clicked the close button.
// Cancel if dialogs are open.
if (dialogsOpen)
{
e.Cancel = true;
}
}
}
Upvotes: 0