简体   繁体   English

C#表单中的Winform表单

[英]C# Winform Forms in Form

i want to open multiple instances of one form and display it in another form or paenel of another form. 我想打开一个表单的多个实例,并以另一种形式或另一种形式的paenel显示它。 how to do it 怎么做

If you're not using MDI, you can still add a form to another form, or to a panel on a form. 如果您不使用MDI,您仍然可以将表单添加到另一个表单或表单上的面板。

public Form1()
{
    InitializeComponent();

    Form2 embeddedForm = new Form2();
    embeddedForm.TopLevel = false;
    Controls.Add(embeddedForm);
    embeddedForm.Show();
}

You will need to set the FormBorderStyle to None , unless you want to have an actual movable form inside your form. 您需要将FormBorderStyle设置为None ,除非您希望在表单中包含实际的可移动表单。

If you want to do this to create a reusable "template" to use in multiple forms, you should consider creating a user control instead. 如果要执行此操作以创建可在多个表单中使用的可重用“模板”,则应考虑创建用户控件。 Not to be confused with a custom control, which is intended for when you need to do your own drawing instead of using collections of standard Windows controls. 不要与自定义控件混淆,后者适用于需要执行自己的绘图而不是使用标准Windows控件集合的情况。

I'm not entirely sure what your intentions are, but MDI (as mentioned in one of the other answers) might actually be what you're looking for. 我不完全确定你的意图是什么,但是MDI(正如其他答案中提到的那样)可能实际上就是你正在寻找的东西。

You should: 你应该:

1) Choose for MDI, that means accepting the complete MDI style for your GUI 1)选择MDI,这意味着接受GUI的完整MDI样式

or 要么

2) Not embed Forms at all, it's better (and easier) to use UserControls. 2)根本没有嵌入表单,使用UserControls会更好(也更容易)。 Try them out before you make a choice. 在做出选择之前尝试一下。 If you do use Forms, make sure shortcut keys etc are all working like you want. 如果您确实使用表单,请确保快捷键等都可以正常工作。

Short intro: You design a UserControl like a Form and then place it on a Form like any other Control. 简介:您像设计一样设计一个UserControl,然后将其放在一个Form上,就像任何其他Control一样。

Form1 fChild = new Form1();
fChild.MdiParent = this;
fChild.Show();

And the IsMDIContainer of the parent should be set to True. 并且父级的IsMDIContainer应设置为True。

For a complete tutorial, you can refer to : Introduction to MDI Forms with C# @ CodeProject . 有关完整的教程,您可以参考: 使用C#@ CodeProject介绍MDI表单

I came across this older thread looking for something similar. 我遇到了这个老线程,寻找类似的东西。 In case anyone else is looking - I used Add New Item >Windows Forms > Inherited Form. 如果其他人正在寻找 - 我使用添加新项目> Windows窗体>继承窗体。 It allows you to choose a form to embed as the starting point for another form. 它允许您选择要嵌入的表单作为另一个表单的起点。 This was is in VS 2015 Community Edition. 这是VS 2015社区版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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