Embedding a winform within a winform (c#)

前端 未结 5 529
我在风中等你
我在风中等你 2020-12-29 08:57

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.

I

5条回答
  •  鱼传尺愫
    2020-12-29 09:47

    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 far you 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.

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

    Where tpgCaseNotes is the control I want Form FrmCaseNotes embedded in.
    In this case a tab page on the Form I am calling from.

提交回复
热议问题