Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Use form as subform

0.00/5 (No votes)
21 Jun 2012 1  
Use existing form as a subform

Introduction

I post this simply trick because I not found nothing with "subform" tag, when I start to design a tabbed windows forms application, I have serious problems to make my application MDI, because I the reports and pop windows hide behind the forms, I make my original design manipulating forms just under a TabControl control, and arrays to determine what windows shows on click tabs, or which tab select based on form selected for menu.

I search solution to my situation but I not found nothing specifical, some day, I found the solution in other forum, searching for another kind of situation.

Background 

Basic idea behind of this, is that you can design and save forms as .cs files, showing each of them in the solution explorer, en enclose them in run time in one (or many, who knows) principal form.

Using the code

You must have form that you want enclose in other form, design the form as you want, add any functionality, then, the only thing that you need is add a container control on you main form, there is a code to generate very basic main form: 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyNameSpace
{
    public partial class Form1 : Form
    {
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(12, 30);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(523, 309);
            this.panel1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(547, 351);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;

        public Form1()
        {
            InitializeComponent();
        }
    }
}

Add a form as sub form 

Supposing that you have a fully functional form, called frmReportsAndCharts, and you want to enclose that form on a main form, use the next simply code: 

frmReportsAndCharts SubForm = new frmReportsAndCharts();
SubForm.FormBorderStyle = FormBorderStyle.None;
SubForm.TopLevel = false;
SubForm.ShowInTaskbar = false;
SubForm.Show();
SubForm.Dock = DockStyle.Fill;
this.panel1.Controls.Add(SubForm);

and you have a sub form! good luck!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here