469,953 Members | 2,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

C# Setting Form Child of another form

464 Expert 256MB
I have form1 and form2. I want form2 to be a child of form1 so that when i set its location it will be drawn in relation to its parent.
EX:
Expand|Select|Wrap|Line Numbers
  1.  Form2Object.Location = new point (0,0)
would put form2 at the top left corner of form one.

I set the MDI property to true for form one. My Created my form2 like so:

Expand|Select|Wrap|Line Numbers
  1. Form2 Form2Object = new Form2();
  2. Form2Object.Parent = this; //causes error see below
  3. Form2Object.Location = new point (0,0); 
  4.  
The error i get is
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: Top-level control cannot be added to a control.
Nov 19 '07 #1
8 7494
Shashi Sadasivan
1,435 Expert 1GB
You could have a form be a child of another if the parent is an MDi Container.
which means that the form will be placed inside the MDI container.
How about passing the form1 object itself to form2 in the constructor and then computing the location for it to be placed in?
Nov 19 '07 #2
Studlyami
464 Expert 256MB
I have set the parent form MDI option. Within the parent form (form1) constructor is where i have
Expand|Select|Wrap|Line Numbers
  1.       Form2 Form2Object = new Form2();
  2.       Form2Object.Parent = this; //causes error see below
  3.       Form2Object.Location = new point (0,0);
  4.  
Within the form1 constructor i could get the rect and then do screen to client and have the window appear where i want it to, but then i would have to do this everytime form1 moves.
Nov 19 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
well....
The issue is because of form1 not exactly being able to be a parent for form2.
Could you please tell what do you mean by MDI Option?
as in the proprty that you set for that. is that isMDIContainer?

Moreover since form2's position is being set from form1 itself, you can disregards setting form2's parent (so delete the line where you set form2.Parent = this;)

It should be fine then.

cheers
Nov 19 '07 #4
Studlyami
464 Expert 256MB
When i say i set the MDI option that is the option under the properties of the form (IsMDIContainer). Right now when i do the Form2.Location = new Point (0,0) it gets drawn at the top left of my monitor not the top left of Form1.
Nov 19 '07 #5
Shashi Sadasivan
1,435 Expert 1GB
When i say i set the MDI option that is the option under the properties of the form (IsMDIContainer). Right now when i do the Form2.Location = new Point (0,0) it gets drawn at the top left of my monitor not the top left of Form1.
0,0 represents the co-ordintaes in the screen.
You might have to use the PointToClient Method

Expand|Select|Wrap|Line Numbers
  1. Form2.Location = this.PointToClient(0,0);
hoping that this form is opened from form1 and this refers to the Form1 object
Hope this works
Nov 19 '07 #6
Plater
7,872 Expert 4TB
Suppose you have this:
Expand|Select|Wrap|Line Numbers
  1. Form1 frm1 = new Form1();
  2. Form2 frm2 = new Form2();
  3. frm1.IsMDIContainer=true;
  4. frm2.Parent=frm1;
  5. frm2.Location=new Point(0,0);
  6. frm1.Show();
  7.  
That *should* work.


Although I generally prefer to do it:
Expand|Select|Wrap|Line Numbers
  1. Form1 frm1 = new Form1();
  2. Form2 frm2 = new Form2();
  3. frm2.Location=new Point(0,0);
  4. frm1.Controls.Add(frm2);
  5. frm1.Show();
  6.  
Nov 19 '07 #7
Studlyami
464 Expert 256MB
Although I generally prefer to do it:
Expand|Select|Wrap|Line Numbers
  1. Form1 frm1 = new Form1();
  2. Form2 frm2 = new Form2();
  3. frm2.Location=new Point(0,0);
  4. frm1.Controls.Add(frm2);
  5. frm1.Show();
  6.  
At the frm1.Controls.Add(frm2)
i get the same error
"An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: Top-level control cannot be added to a control."

I was able to get it to work. My problem was the
Expand|Select|Wrap|Line Numbers
  1. frm1.Parent = this   //This line has to be 
  2. frm1.MdiParent = this;
  3.  
What advantage is there between setting the frm2 parent vs the frm1.Controls.Add(frm2)?
Nov 20 '07 #8
Plater
7,872 Expert 4TB
What advantage is there between setting the frm2 parent vs the frm1.Controls.Add(frm2)?
Not sure, other then that you can treat the form like any other control. You can tell the form not to draw it's status bar and will be just like you added a panel to your form.
Nov 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by jaYPee | last post: by
6 posts views Thread by Leszek | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.