C#:只有顶级控件才能拥有所有者。 (地铁框架)

问题描述 投票:0回答:1

我得到这个Only top-level controls can have an owner.异常,即使我已经将我的子表格设置为错误。

这是我的主要表单的代码:

    public Form childform;
    public void innerChild(Form child)
            {
                breaker();
                childform = child;
                childform.TopLevel = false;
                splitContainer1.Panel2.Controls.Add(childform);
                childform.Show(); //**Toplevel e**
            }

    private void breaker()
            {
                try
                {
                    childform.Close();
                    childform.Dispose();
                }
                catch { }
            }

     private void btnSupProd_Click(object sender, EventArgs e)
            {
                innerChild(new Supplier_Supplies(this));
}

这是我孩子表格中的相关代码:

MySqlConnection conn;
public Dashboard reftomain;
public Supplier_Supplies(Dashboard main)
        {
            InitializeComponent();
            reftomain = main;
            this.TopLevel = false;
            conn = new MySqlConnection("server=localhost; database=sample; uid=root; pwd=");
        }

我正在尝试将子表单显示在主表单的面板中。但这个错误一直持续存在,有帮助吗?

堆栈跟踪:

System.ArgumentException occurred
  HResult=0x80070057
  Message=Only top-level controls can have an owner.
Parameter name: value
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.Form.set_Owner(Form value)
   at MetroFramework.Forms.MetroForm.MetroShadowBase..ctor(Form targetForm, Int32 shadowSize, Int32 wsExStyle)
   at MetroFramework.Forms.MetroForm.MetroFlatDropShadow..ctor(Form targetForm)
   at MetroFramework.Forms.MetroForm.CreateShadow()
   at MetroFramework.Forms.MetroForm.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.Show()
   at Jacam_Merchat.Dashboard.innerChild(Form child) in C:\Users\User\Desktop\SAD 2\Jacam Merchat\Jacam Merchat\Dashboard.cs:line 37
   at Jacam_Merchat.Dashboard.btnSupProd_Click(Object sender, EventArgs e) in C:\Users\User\Desktop\SAD 2\Jacam Merchat\Jacam Merchat\Dashboard.cs:line 64
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at MetroFramework.Controls.MetroTile.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.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.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 System.Windows.Forms.Application.Run(Form mainForm)
   at Jacam_Merchat.Program.Main() in C:\Users\User\Desktop\SAD 2\Jacam Merchat\Jacam Merchat\Program.cs:line 19
c# forms parent-child parent
1个回答
3
投票

测试出来了。

在子表单上将shadowtype设置为none

希望这可以帮助。 快乐的编码。

© www.soinside.com 2019 - 2024. All rights reserved.