在 winform 中嵌入一个 winform (c#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/301678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:18:10  来源:igfitidea点击:

Embedding a winform within a winform (c#)

c#winforms

提问by phrenetic

Is it possible to embed a windows form within another windows form?

是否可以在另一个窗体中嵌入一个窗体?

I have created a windows form in Visual Studio along with all its associated behaviour.

我在 Visual Studio 中创建了一个 Windows 窗体及其所有相关的行为。

I now want to create another windows form containing a tab view, and I want to embed the first windows form into the tab view. Is this possible?

我现在想创建另一个包含选项卡视图的窗口窗体,并且我想将第一个窗口窗体嵌入到选项卡视图中。这可能吗?

采纳答案by Mendelt

Not directly. You can create a usercontrol, move all of the code from your form to the usercontrol and use this in both forms. You might need to change some of the code from your form but probably not much.

不直接。您可以创建一个用户控件,将所有代码从表单移动到用户控件并在两种表单中使用它。您可能需要更改表单中的某些代码,但可能不会太多。

回答by jkchong

You could try the SetParent() API call, although I have not verified that it would work myself. If that does not work, Mendlet's solution above is probably your best option.

您可以尝试 SetParent() API 调用,但我还没有验证它是否可以自己工作。如果这不起作用,上面的 Mendlet 解决方案可能是您的最佳选择。

回答by ConcernedOfTunbridgeWells

The way to do this is with a user control rather than a form. This is what user controls are for. This technique can be used for quite a lot of user interface tricks such as wizards (the controls can be shared between the wizard and other parts of the application), explorer style browsers with a tree control and controls swapped out based on the selected node.

这样做的方法是使用用户控件而不是表单。这就是用户控件的用途。这种技术可用于相当多的用户界面技巧,例如向导(控件可以在向导和应用程序的其他部分之间共享)、带有树控件的资源管理器样式浏览器和基于所选节点换出的控件。

I have done quite a lot of work with application architectures that use user controls for everything and frameworks for explorers, wizards and other types of forms (even going back to VB6). As an approach, it works very well.

我在应用程序架构方面做了很多工作,这些应用程序架构使用用户控件来控制一切,以及浏览器、向导和其他类型的表单(甚至回到 VB6)的框架。作为一种方法,它非常有效。

回答by Refracted Paladin

Disclaimer

免责声明

This will work as I am using it in my application extensively. That being said I would pursue the User Control route as depending on how faryou carry the embedding things start to flake out. FYI

这将起作用,因为我在我的应用程序中广泛使用它。话虽如此,我会追求用户控制路线,因为这取决于您携带嵌入的东西开始剥落的距离。供参考



Yes this is possible. This is how:

是的,这是可能的。这是如何:

public static void ShowFormInContainerControl(Control ctl, Form frm)
{
    frm.TopLevel = false;
    frm.FormBorderStyle = FormBorderStyle.None;
    frm.Dock = DockStyle.Fill;
    frm.Visible = true;
    ctl.Controls.Add(frm);
}

I have that in a Class Library and then I call it like so from the FORM I want to embed.

我在一个类库中有它,然后我从我想要嵌入的 FORM 中这样称呼它。

public FrmCaseNotes FrmCaseNotes;
FrmCaseNotes = new FrmCaseNotes();
WinFormCustomHandling.ShowFormInContainerControl(tpgCaseNotes, FrmCaseNotes);

Where tpgCaseNotesis the control I want Form FrmCaseNotesembedded in.
In this case a tab page on the Form I am calling from.

tpgCaseNotes我希望 FormFrmCaseNotes嵌入的控件在哪里。
在这种情况下,我正在调用的 Form 上的选项卡页。

回答by LBQC

let's say you have 2 projects win1 and win2. both are winform projects. you look for embeding win2 in win1.

假设您有 2 个项目 win1 和 win2。两者都是winform项目。你寻找在 win1 中嵌入 win2。

solution:

解决方案:

open the win2 project and change the output type to "Class Library" (in Application tab)

打开 win2 项目并将输出类型更改为“类库”(在应用程序选项卡中)

open the project win1, and add the win2 dll project as a ref in win1 project go in the win1 code, and put this :

打开项目win1,将win2 dll项目作为ref添加到win1项目中的win1代码中,并输入:

        win2.Form1 formI = new win2.Form1();
        formI.TopLevel = false;
        formI.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        formI.Size = this.Size;
        formI.BringToFront();
        formI.Visible = true;
        this.Controls.Add(formI);