Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 395
CaTx Avatar asked May 24 '12 22:05

CaTx


1 Answers

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

like image 145
Grzegorz W Avatar answered Oct 15 '22 08:10

Grzegorz W