English 中文(简体)
设置窗体为“ 父母丢弃例外 ” “ 无法将控制添加到控制中 ”
原标题:Set form as Parent throw exception "Top-level control cannot be added to a control"

我要从其他窗体中访问窗体的变量。 点击主窗体中的按钮时, 我要将主窗体设置为父体, 然后将另一个窗体( 子窗体) 显示为主窗体的变量。 我的点击处理程序如下:

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();
}

它编译得很好,没有错误。 但是, 当我运行主窗体并点击系统按钮时, 它会给我一个例外。 我用同样的按钮点击其它代码( 不是我的代码) 做类似的事情, 并且不会遇到错误( 仅仅将主窗体设置为父体 ) 。

我做错什么了?

最佳回答

最佳方式是使用 EP.ShowDialog(这) 并随后使用 所有者 财产。

问题回答

您需要设置 EP.Toplevel 属性为假 。 它会允许您设置其父性 。

进一步阅读。

如果您只想要访问其它形式的变量和控件, 那么也许你可以以其他方式达到它, 而不是通过父母关系。

OK, apparently the way to do it is to call

Form_Child.ShowDialog(this)

然后我可以打电话

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

或者如果我在命名空间属性中定义变量

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

有两种方式。

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

这样尝试对我有用。 您需要将主法设置为Mdicontainer = 真实的窗体属性

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".



<强> 要完成此任务:

• 禁用主形式“是Mdicontainer”财产(该财产的使用还是过时的)。

• 将表单顶部属性设置为真实。

• 你现在应该能够完成你的特点。


**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, (to achieve perfection) is to check the MainForm > WindowState; and create a code block to minimize or bring the Forms to their specific state.

<强 > e:

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

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

以此写, 在父窗体中心进行对话框显示 。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签