Click here to Skip to main content
15,879,053 members
Articles / Desktop Programming / Win32
Tip/Trick

How to Host Windows Form in Windows User Control

Rate me:
Please Sign up or sign in to vote.
4.64/5 (6 votes)
30 Apr 2013CPOL1 min read 19.9K   368   7   3
In the tip, I want to illustrate how to host windows form in windows user control

Introduction

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.

Background

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:

C#
public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
    }

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

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

C#
public partial class UserControl1: UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ChildForm childForm = new ChildForm();
        childForm.Show(this.panel1);
    }
} 

Congratulations. Please run your project and click on Button1.

History

  • May 1, 2013: First published

License

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


Written By
Software Developer (Senior) BHGE
Germany Germany
I worked as a software engineer and researcher in different countries with a wide range of related projects and engineers from all around the world. I was involved in Oil&Gas, Telecommunication, Transportation, and Semiconductor projects and played various roles such as junior, senior, and lead engineer both in embedded and non-embedded devices and technologies.

During my professional carrier, I was directly involved in designing and maintaining editor, compiler, and interpreter for IEC 611131-3 (PLC programming standard) and fault-tolerant communication layer for distributed automation standard IEC 61499, and many other projects such as DCS (Distributed Control Systems), (SCADA) Supervisory Control and Data Acquisition System, Oilfield (CMS) Computerised Maintenance Systems, Oil&Gas Laboratory Automaton Systems, and Semiconductor Equipment Connectivity Solutions.

Currently, I pursue a Ph.D. degree in Computer Science in the Technical University of Dresden and works as a software engineer in Germany. Beside, I am a certified specialist in Microsoft technologies since 2011.

My main research and work areas are Industrial Communication and Automation Systems, Real-Time Systems, Service-Oriented Systems, IEC 61131-3, IEC 61499, and Distributed Embedded Systems.

Comments and Discussions

 
QuestionWhaaatt !? Pin
Ashok Kumar RV3-Sep-18 0:47
Ashok Kumar RV3-Sep-18 0:47 
QuestionInteresting Pin
adriancs30-Apr-13 23:27
mvaadriancs30-Apr-13 23:27 
AnswerRe: Interesting Pin
Aydin Homay30-Apr-13 23:31
Aydin Homay30-Apr-13 23: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.