C# 将表单设置为父级抛出异常“无法将顶级控件添加到控件中”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10746053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:04:52  来源:igfitidea点击:

Set form as Parent throw exception "Top-level control cannot be added to a control"

c#formsexceptionparent

提问by CaTx

I want to access variables of a form from another form. On clicking a button inside my Main form, I want to set my Main form as Parent, then bring up another form (child form) wherein I will access variables of the Main form. My click handler is as follow:

我想从另一个表单访问一个表单的变量。单击主窗体中的按钮时,我想将主窗体设置为父窗体,然后调出另一个窗体(子窗体),我将在其中访问主窗体的变量。我的点击处理程序如下:

private void btnSystem_Click(object sender, EventArgs e)
{
    Form_EnterPassword EP = new Form_EnterPassword();
    EP.Parent = this;        //error: Top-level control cannot be added to a control
    EP.ShowDialog();
}

It compiles fine without any error. However, when I run the Main form and click on the System button, it throws me an exception. I do something similar in another code (not mine) with the same button click, and encounter no error (just with setting Main form as Parent).

它编译得很好,没有任何错误。但是,当我运行主窗体并单击系统按钮时,它会引发异常。我在另一个代码(不是我的)中用相同的按钮单击做了类似的事情,并且没有遇到任何错误(只是将主窗体设置为父级)。

What am I doing wrong? Is there something in my Main code that cause this?

我究竟做错了什么?我的主要代码中是否有导致此问题的内容?

采纳答案by Grzegorz W

Best way would be to use EP.ShowDialog(this)and later use Ownerproperty.

最好的方法是使用EP.ShowDialog(this)和以后使用Owner财产。

回答by Matzi

You need the EP.TopLevelproperty to be set to false. It will let you to set a parent to it.

您需要将该EP.TopLevel属性设置为 false。它会让你为它设置一个父级。

Further reading.

进一步阅读。

In case you only want to access variables and controls of another form, then maybe you can reach it in other ways, not trough a Parent relationship.

如果您只想访问另一种形式的变量和控件,那么也许您可以通过其他方式访问它,而不是通过 Parent 关系。

回答by CaTx

OK, apparently the way to do it is to call

好的,显然这样做的方法是打电话

Form_Child.ShowDialog(this)

and then I can call

然后我可以打电话

FromParent_aVariable = ((Form_Parent)this.Owner).aVariable;

or if I define aVariable in the namespace Properties then

或者如果我在命名空间属性中定义了一个变量,那么

FromParent_aVariable = NameSpace.Properties.Settings.Default.aVariable;

there are two ways.

有两种方法。

回答by bharath

Writing this way, made the dialog display on the center of the parent form.

以这种方式编写,使对话框显示在父窗体的中心。

   Form_Child.StartPosition = FormStartPosition.CenterParent;
   Form_Child.ShowDialog(this);

回答by Wady Nicudemos

Form_EnterPassword EP = new Form_EnterPassword();
EP.MdiParent = this;       
EP.Show();

try this way, it helps for me. you need to set principalform as isMdicontainer = true at the form properties

试试这种方式,它对我有帮助。您需要在表单属性中将 principalform 设置为 isMdicontainer = true

回答by Switch

I had a similar situation recently. I was attempting something similar but by controlling the Child Forms from a different class.

我最近也有类似的情况。我正在尝试类似的事情,但通过控制来自不同类的子表单。

Note(s):You're trying to set the Child Form(s) "TopMost" to something that does not allow it.
In this case the "MdiContainer".

注意:您试图将子表单“TopMost”设置为不允许的内容。
在本例中为“MdiContainer”。


To accomplish this:


要实现这一点:

? Disable MainForm "isMdiContainer" property (its use is kind of obsolete anyway).

? 禁用 MainForm "isMdiContainer" 属性(无论如何它的使用已经过时了)。

? Set the Form(s) TopMost properties to true.

? 将 Form(s) TopMost 属性设置为 true。

? You should now be able to accomplish your feature.

? 您现在应该能够完成您的功能。


Code Example:


代码示例:

/* On your Main Form Class */

private void btnSystem_Click(object sender, EventArgs e)
{
    // Instantiate the Form_EnterPassword by passing the MainForm
    Form_EnterPassword EP = new Form_EnterPassword(this);
    EP.Show(); // No longer as modal Form to display in front.
}

/* Under your EnterPassword Form Class */

// Do not create a new Instance of MyMainForm.
// You want to use the same thread as your MainForm
private MyMainForm mainForm;

/* Constructor */
public Form_EnterPassword(MyMainForm form)
{
    mainForm = form;
    this.Owner = mainForm; // "this" refers to the: EnterPassword Form.
}


Remarks:The only additional thing that you (may) have to do is to to achieve perfection is to check the MainForm WindowState; and create a block to minimize or bring the Forms to their specific state.


备注:您(可能)需要做的唯一额外的事情就是检查 MainForm WindowState;并创建一个块以最小化表单或将表单置于其特定状态。

i.e:

IE:

if (WindowState == FormWindowState.Minimized)
{ /* Code to Minimize all the Child Forms. */ }

else { /* Code to bring all Forms to their "Normal" State */ }