设置MdiChild属性

时间:2019-07-18 14:51:06

标签: vb.net winforms

我有一个父表单frmParent和两个MdiChild表单(frmChild1frmChild2)。

当我这样做时:

frmChild1.Show()通过frmParent中的按钮正确显示。

当我这样做时:

Dim frmChild As New frmChild1()
frmChild.Show()

它仍然可以正常工作。

当我这样做时:

Dim frmChild As New frmChild1()
frmChild.MdiParent = Me
frmChild.Show()

我收到错误消息:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll

Additional information: Error creating window handle.

我在其他地方查看了此错误,大多数人认为这与创建过多的Handle或GDI对象有关,但是您可以看到并非如此:

Handles and GDI Objects for process running in debug mode via VS2017

我对如何诊断其原因一无所知。

我还有另一个bug,它不会触发frmChild1.Click事件,我怀疑这可能是相关的(或可能不是!)。

关于窗口的任何想法都会处理错误?如果有用的话,我在下面提供了堆栈跟踪:

   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Form.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.Show()
   at Application.frmParent.tsmiMdiChild1_Click(Object sender, EventArgs e) in \redacted_location\frmParent.vb:line 90
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Application.My.MyApplication.Main(String[] Args) in :line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

1 个答案:

答案 0 :(得分:0)

经过反复试验,我找到了答案。希望其他人觉得这很有用。

我在做:

Dim frmChild As New frmChild1()
frmChild.MdiParent = Me
frmChild.Show()

然后:

Private Sub frmChild_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Hide()

    'Other code

End Sub

一旦停止在子窗体的加载事件中隐藏它,我将不再收到错误。

我不完全理解为什么在.Show()之前设置MdiParent,然后在子窗体的load事件中设置Hide()的组合会导致此问题(不是完全愚蠢的)。在我的代码中出现此逻辑的原因是由于对表单的加载方式进行了一些重构期间的疏忽。

关于我的其他问题:

  

我还有另一个bug,该错误导致frmChild1.Click事件不会触发,   我怀疑这可能是相关的(或可能不是!)。

事实证明,如果在调用MdiParent之后设置.Show()属性,除非单击标题栏(我误认为这是鼠标单击事件),否则它似乎不会将焦点集中在其他子窗体上被吞下)。

现在我将MdiParent设置为.Show()之前,这两个方面都很好。