简体   繁体   中英

Open form within a form C#

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM