Close an MDI child window

  • Thread starter Thread starter Guy Babbitt
  • Start date Start date
G

Guy Babbitt

I have an MDI application that starts an instance of a child form at
application start. I have an event handler on a combo box checking
for the selected value to change. When the select value changes, I
call a method on the parent form to change some menu options, and then
I need to close the child form. My code looks something like this:

private void ddlDatabase_SelectedIndexChanged(object sender,
System.EventArgs e)
{
_parent.BuildMenus();
this.Close();
}
The problem is, writing the code this way causes an exception to be
thrown at the following block of code:

static void Main()
{
Application.Run(new MDIParent());
}

on the parent form. I have tried a variety of things, but am not sure
of the right way to go about this.

Can someone shed some light for me?

Guy
 
Guy Babbitt said:
private void ddlDatabase_SelectedIndexChanged(object sender,
System.EventArgs e)
{
_parent.BuildMenus();
this.Close();
}
The problem is, writing the code this way causes an exception to be
thrown at the following block of code:

Without knowing what BuildMenus() does, have you tried putting a DoEvents()
between BuildMenus() and Close()? If BuildMenus() ends up sending messages
to the MDI children I could see why perhaps things could go awry.

-- Alan
 
Let me simplify it like this:

On a child MDI form I have a combo box. For the selectionchanged
event of that combo box I want to simply do a this.Close(). This
action ends up bubbling an error to the parent form with the following
exception:

System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)\r\n at
System.Windows.Forms.Control.DefWndProc(Message& m)\r\n at
System.Windows.Forms.Control.WmCommand(Message& m)\r\n at
System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.ComboBox.WndProc(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)\r\n at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.Application.Run(Form mainForm)\r\n at
FSA.FoodShow.Win32.MDIParent.Main() in mdiparent.cs:line 385


I had a hunch that this might be because I was placing the .close()
command in an event handler that was currently firing, so I took the
approach of actually creating a custom event that the parent MDI
subscribes to and then had the combo box selectionchange event call
that new event. I had the event handler for the new event then try to
close the child window, but that still thows the same error.

So, how am I supposed to have a MDI child close itself?

Any thoughts?

Guy
 
Back
Top