I'm wondering, is it possible to open a form within a form in C#? I mean for example, in form 1, there's a link that says: "Open form 2", when a user clicks that link, "Form 2" will open in the same form. It's just like navigation through different forms. I need some codes to get started. Thanks in advance.
Asked
Active
Viewed 7,255 times
1 Answers
3
You can host a form in another control (ie. a Panel). Set the TopLevel property of the form false and set your host control Content to an instance of your Form.
Example code:
var childForm = new ChildForm() {TopLevel = false, Visible = true};
while (hostPanel.Controls.Count > 0) hostPanel.Controls[0].Dispose();
hostPanel.Controls.Add(childForm);
Edit:
I assuming you want to show a form inside another form (control).
Showing another form(in a new window) is as easy as invoking the Show() / ShowDialog() methods on an instance of it as @Marco mentioned in his answer.
-
Never use Controls.Clear(), it permanently leaks controls and window handles. – Hans Passant Nov 05 '11 at 08:06