Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm new to Visual C#. I'm writing an application that will access a database. When the View button is clicked I want another form to launch so that information can be entered in the child form, instead of the parent form. How do you launch a child form from the parent form? I have Googled for the answer but haven't had any luck.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Jun-11 17:22pm    
Sharpen your Google skills; better yet, read regular MSDN help pages -- on Form and everything else. Don't look at "answers", you may not find them; pay attention for who things works.
See my solution. Better now?
--SA
BobJanova 24-Jun-11 6:27am    
As mentioned in SA's solution, the terminology in .Net is not 'parent/child' but 'owner/owned'. Perhaps this was causing you searching problems.

One ... simplified ... strategy to show a second Form when your 'View' button is clicked on your main Form:
// inside the main Form class definition
private Form2 viewForm;

// inside the Load event of the main Form
viewForm = new Form2();
viewForm.Owner = this;

// inside the Click event of the 'View Button
// handle the odd behavior of an 'independently' minimized owned Form
if (viewForm.WindowState == FormWindowState.Minimized)
{
    viewForm.WindowState = FormWindowState.Normal;
}
viewform.Show();
things to consider in your solution:

0. think about getting a good WinForms book for a thorough review of child-forms, owned-forms, MDI, multi-document form architecture, effects of FormBorderStyle, FormWindowState, ControlBox, etc. my favorite authors are Jesse Liberty, Matthew McDonald, and Chris Sells.
1. whether or not to have the owned form have minimize, maximize, control box, adornments
2. whether or not to have the owned form show in the TaskBar
3. whether or not to create the owned form once, and never allow it to be closed, or, if you do allow it to be closed, what you need to do to re-create it ad hoc

detailed comments:

1. There is, indeed, a possible child-parent relationship between Forms: A child Form of another Form is visually contained inside its parent Form. This relationship is a fundamental part of the Control object from which Forms inherit.

2. The owner-owned relationship simply means that owned Forms are displayed, hidden, minimized, etc., with their owner Form. Typically this relationship is used for a "floating window," or when you don't want any other Form to get visually "between" the shown Form and its owned Forms.

Note: both owner and owned Forms can be moved anywhere on the screen, can be re-sized, independently of each other. The owned Form can be minimized independently of its owner Form, but minimizing the owner Form will automatically minimize all owned Forms (and they will not appear in the TaskBar: even when the ShowInTaskBar property of the owned Forms is set to 'true).

3. The example code from SAKryukov above, under the heading "more accurate code" ... if used inside the Program.cs file of a Windows Form project ... will build, but will cause an error at run-time when you close the MainForm: it will never open Form2:

a. when Application.Run opens the MainForm it enters an event-loop that persists until MainForm is disposed.

b. when MainForm is disposed ... when it is closed by a user ... then, and only then, is any code after Application.Run executed.

c. so, at the moment it reaches the line where adding the new Form2 to the owned Forms of MainForm: MainForm no longer exists, and the application will throw an error.
 
Share this answer
 
v7
There is no child-parent relationships between forms. (There is Owned and owner form which I highly recommend to use).

Also, one form is main, another is not. Look at your entry point (normally, "Main"). It has the line:

C#
Application.Run(new Form1());


This start of application defines what is your main form. When you close a main form, the whole application quits (unless you handle FormClosing event to cancel it).

If you want to show other forms, you need to… well, show them:

C#
//pretty sloppy code:
Form newForm = new Form2();
newForm.Show();

//more accurate code:
Form mainForm = new Form1();
Application.Run(mainForm);
Form newForm = new Form2();
mainForm.AddOwnedForm(newForm);
newForm.ShowInTaskbar = false;
newForm.Show();


Making a form owned by a main form makes them activate together, which help integrity if application behavior. In this way, no window from other application can appear in Z order between the forms of your application. Not showing a form in a task bar complements this behavior. In this way, only a main form represents whole application it the task bar.

See: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^].

—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 23-Jun-11 17:23pm    
Excellent answer, but I think the OP needs to learn how to use google if he in fact couldn't find this basic of basic tasks on there.
Sergey Alexandrovich Kryukov 23-Jun-11 17:37pm    
Thank you, Marcus.
As to using Google, I dedicated a comment to the question on top of the page. Of course: people are trying to find "answers" the their problems. This is not very productive; and a cookbook recipe is nothing like real knowledge.
--SA
Kim Togo 24-Jun-11 3:54am    
Good answer SA. My 5.
Sergey Alexandrovich Kryukov 24-Jun-11 4:02am    
Thank you, Kim.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900