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

余生长醉 提交于 2019-12-05 01:32:26

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

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

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.

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.

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

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

   Form_Child.StartPosition = FormStartPosition.CenterParent;
   Form_Child.ShowDialog(this);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!