none
System.InvalidOperationException “Window handle already exists” RRS feed

  • 问题

  • private void ChartFormPinSetting(Form form,bool IsStarting)
        {
          form.TopLevel = false;
          form.MdiParent = null;
          Controls.Add(form);
          if (IsStarting)
          {
            form.Show();
            form.BringToFront();
          }
          else if (form.Visible)
          {
            form.Show();
            form.BringToFront();
          }
        }
    
        private void ChartFormUnPinSetting(Form form)
        {
          //MessageBox.Show()
          Controls.Remove(form);
          form.Parent = null;
          form.MdiParent = this;
          if (form.Visible)
          {
            form.BringToFront();
          }
        }
    



    这个异常困扰我很久了。都没解决掉。我看了下手册。
    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.form.mdiparent.aspx
    应该是这个: "The Form assigned to this property is located on a different thread. ".

    但是我不知道为什么。和怎么改。有人能帮帮忙吗?

    System.InvalidOperationException was unhandled by user code
     Message="Window handle already exists."
     Source="System.Windows.Forms"
     StackTrace:
        at System.Windows.Forms.NativeWindow.CheckReleased()
        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.Form.set_MdiParentInternal(Form value)
        at System.Windows.Forms.Form.set_MdiParent(Form value)
    Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.OnToolClick(ToolClickEventArgs e)
        at Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.FireEvent(ToolbarEventIds id, EventArgs e)
        at Infragistics.Win.UltraWinToolbars.StateButtonTool.set_Checked(Boolean value)
        at Infragistics.Win.UltraWinToolbars.StateButtonTool.OnToolClick()
        at Infragistics.Win.UltraWinToolbars.ToolBase.OnToolClick()
        at Infragistics.Win.UltraWinToolbars.ButtonToolUIElement.DoClickProcessing(MouseEventArgs e)
        at Infragistics.Win.UltraWinToolbars.ButtonToolUIElement.OnMouseUp(MouseEventArgs e)
        at Infragistics.Win.ControlUIElementBase.ProcessMouseUpHelper(Object sender, MouseEventArgs e)
        at Infragistics.Win.ControlUIElementBase.ProcessMouseUp(Object sender, MouseEventArgs e)
        at Infragistics.Win.Utilities.ProcessEvent(Control control, ProcessEvent eventToProcess, EventArgs e)
        at Infragistics.Win.UltraControlBase.OnMouseUp(MouseEventArgs e)
        at Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea.OnMouseUp(MouseEventArgs e)
        at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
        at System.Windows.Forms.Control.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     InnerException: 


    2011年9月1日 7:37

答案

  • 这貌似不是用户代码的问题,从 StackTrace 看象是 Infragistics 第三方控件没有正确释放窗体句柄的原因。找控件提供商问问吧。这是 Native Window 级别的问题。用户代码可能无能为力的。
    Mark Zhou
    2011年9月1日 9:02

全部回复