472,953 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,953 software developers and data experts.

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 7780
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

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

Similar topics

12
by: andreas | last post by:
Hi, A button in a child form creates another form with a textbox showing with show. I set the taborder on first on this textbox in the design and set even the focus by loading but i see no...
4
by: jaYPee | last post by:
I know how to open a form from another form. Say open form2 from form1 using a command button. But my problem is everytime I clicked the button it open again another instance of that form. ...
5
by: Mrozu | last post by:
Hi I have frm1. On this form button.Click code for this button is: Dim frm2 as New frm2 frm2.show So after click, frm2 form is shown.
6
by: Leszek | last post by:
Hi. I wrote a script: function zmiana(ile){ while(document.getElementById('accomp').childNodes.length>1){ ostatni=document.getElementById('document.dane.accomp').lastChild;...
2
by: gazelle04 | last post by:
Can I send strings from a form to another form. I have a button on the first form that opens up a data entry form but needs a data from the first form. I've read in the book about that OpenArgs but...
0
by: hellopraveen30 | last post by:
Here is my form lap and i want to submit data of this form to another form ("http://www.lapkokcasuals.net/jobs/Apply.aspx") without going their how can i do it ihave written these code below data is...
4
by: Mucker | last post by:
I need to open an MDI child form in a C# MDI application. The problem is that this MDI child form is loaded via reflection from a Delphi.NET assembly. Despite my best efforts, this just does not...
1
by: kenneth6 | last post by:
1. In MSVC++ 2005 Windows Form application, how can i pass the variable from 1 form to another form? 2. if, in 1 of the forms, i created a class object in constructor, how can i pass the object to...
1
by: menyki | last post by:
am still finding it difficult as a new java user. am designing a software that has multiple forms, controls such as combobox and so on and finding it difficult to do the following. i want some...
4
by: kkshansid | last post by:
i want to pass 2 variable from one form to another form one is section name which is the column value which is working problem is that i also want another variable culumn name so both variable should...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.