Embedding a winform within a winform (c#)

前端 未结 5 537
我在风中等你
我在风中等你 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:41

    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.

    0 讨论(0)
  • 2020-12-29 09:42

    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.

    0 讨论(0)
  • 2020-12-29 09:42

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

    solution:

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

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

            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);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 09:48

    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.

    0 讨论(0)
提交回复
热议问题