Click here to Skip to main content
13,358,079 members (57,555 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 30 Apr 2013

How to Host Windows Form in Windows User Control

, 30 Apr 2013
Rate this:
Please Sign up or sign in to vote.
In the tip, I want to illustrate how to host windows form in windows user control


In my previous tip, "Hosting windows form user control in MFC dialog", we discussed about how to host Windows Form user controls in MFC dialog box and integrate MFC with .NET platform with CLI technology. Now, we'll talk about How to host Windows Form in Windows user control. My idea of this tip/trick and previous tip/trick is to reach a solution for using Windows Forms in MFC dialog and integrate .NET Forms with MFC forms.


Before reading this tip/trick, please read about user control from here.

Using the Code

Note: If I have a Windows Form in my project, I could not set parent before initializing Form "TopLevel" properties with false value, and if I do it (set parent object before than), I get an Error.

  1. Open Visual Studio
  2. Create Windows Forms Control Library and set then name of project, for example. "WinFormUserControl".
  3. Drag button control to the UserControl1 place and set Dock property to "Button".
  4. Drag panel control to the UserControl1 place and set Dock property to "Fill".
Now we have a Windows Forms Control Library project with one User Control, Button and Panel Control. The name of the user control is UserControl1 and panel is Panel1. After that, we add a Windows Form in our project, we can set the name of new form to "ChildForm". At this step, we can overload the Show() method of form as follows:

public partial class ChildForm : Form
    public ChildForm()

    public void Show(Panel parentObject)
        if (parentObject != null)
            this.TopLevel = false;
            this.Parent = parentObject;

Now we should instantiate "ChildForm" in button1 click() event and call overloaded Show(Panel parentObject) method for hosting Windows Form in panel.

public partial class UserControl1: UserControl
    public UserControl1()

    private void button1_Click(object sender, EventArgs e)
        ChildForm childForm = new ChildForm();

Congratulations. Please run your project and click on Button1.


  • May 1, 2013: First published


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aydin Homay
Software Developer (Senior) CM S. A.
Portugal Portugal
I am an eternal programmer.

You may also be interested in...


Comments and Discussions

QuestionInteresting Pin
adriancs1-May-13 0:27
mvpadriancs1-May-13 0:27 
Hey this is interesting. Thanks for sharing.
I never though that this could be so easy.
AnswerRe: Interesting Pin
memberAYDIN EBRAHIMI HOMAY1-May-13 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 1 May 2013
Article Copyright 2013 by Aydin Homay
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid