473,729 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Setting Form Child of another form

464 Recognized Expert Contributor
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.Argumen tException' occurred in System.Windows. Forms.dll

Additional information: Top-level control cannot be added to a control.
Nov 19 '07 #1
8 7837
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 Recognized Expert Contributor
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.A dd(frm2)
i get the same error
"An unhandled exception of type 'System.Argumen tException' 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.A dd(frm2)?
Nov 20 '07 #8
Plater
7,872 Recognized Expert Expert
What advantage is there between setting the frm2 parent vs the frm1.Controls.A dd(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
1326
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 cursor. I see the cursor only when i click in the textbox. Why and what to do for to see immediately the cursor in the textbox ?
4
9861
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. here's my code Dim Course As New Course Course.Show() My question is on how to prevent a form from opening again if it is
5
2448
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
6334
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; document.getElementById('document.dane.accomp').removeChild(ostatni);
2
8756
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 it can send only one string, can I send multiple strings to another form?
0
1129
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 submitted but where it is goin i dont know.please help me and give some reponses. Imports System.Object 'Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.IO.IOException Imports foxtrot.xray.WebRobot Imports...
4
10295
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 work. At All. Period. Regular delphi.net forms are fine, MDI children are most certainly not. But I really, really, really need this to work. So, I'm, trying to find a different approach. Is there any way to embed a form in another form? For...
1
1718
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 another form?
1
1683
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 combobox in the software to fill at design time, i.e. two combobox the first one already has 2 items, i want a situation whereby an item selected from the first combobox determines what items will fill the second combobox. secondly, i want to...
4
1529
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 make a suitable query select * from student where $columnname=$columnvalue
0
8928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8766
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9217
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9158
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6026
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2698
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.