65.9K
CodeProject is changing. Read more.
Home

Use Form as Subform

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (3 votes)

Aug 29, 2012

CPOL
viewsIcon

16218

This is an alternative for "Use form as subform"

Introduction

This is a shortcut to the original trick from Antonio Lopez R: Use form as subform

Background

I highly recommend that you read Antonio's article and understand it. I love embed common functions into extensions, because the code becomes very easy to read.

At first, I'll try this way:

static class SubFormExtensions
{
    internal static bool AttachTo(this Form subForm, Panel panel)
    {
        if (panel == null || subForm == null) return false;
        subForm.FormBorderStyle = FormBorderStyle.None;
        subForm.TopLevel = false;
        subForm.ShowInTaskbar = false;
        subForm.Show();
        subForm.Dock = DockStyle.Fill;
        panel.Controls.Add(subForm);
    }
}

Using the Code

What I'm doing here is just using extensions to make things clear. Using the same example as Antonio, you can call this way:

var SubForm = new frmReportsAndCharts();
SubForm.AttachTo(panel1);

Points of Interest

This is my first test writing an article, so feel free to point anything strange here.

History

2012-06-12: First version of article.