Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Tip/Trick

Use form as subform

Rate me:
Please Sign up or sign in to vote.
3.43/5 (7 votes)
21 Jun 2012CPOL1 min read 59.2K   10   10
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: 

C#
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: 

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Mexico Mexico
I'm a cowboy coder,
and I'm proud of that,
If you face a gunslinger
be quick or be dead.

Comments and Discussions

 
GeneralMy vote of 1 Pin
BillWoodruff16-Dec-14 22:13
professionalBillWoodruff16-Dec-14 22:13 
GeneralRe: My vote of 1 Pin
Antonio Lopez R30-Dec-14 16:48
Antonio Lopez R30-Dec-14 16:48 
GeneralRe: My vote of 1 Pin
Antonio Lopez R3-Jun-20 13:29
Antonio Lopez R3-Jun-20 13:29 
Question limits of this way Pin
oula alsheikh16-Dec-14 3:09
oula alsheikh16-Dec-14 3:09 
AnswerRe: limits of this way Pin
Antonio Lopez R30-Dec-14 16:51
Antonio Lopez R30-Dec-14 16:51 
GeneralThis is exactly what I needed! Pin
_onTy_5-Feb-14 20:08
_onTy_5-Feb-14 20:08 
QuestionUserControl Pin
Brad Bruce23-Jun-12 7:21
Brad Bruce23-Jun-12 7:21 
AnswerRe: UserControl Pin
Antonio Lopez R23-Jun-12 19:31
Antonio Lopez R23-Jun-12 19:31 
Depends on your form mate, and on what you want to UserControl do, I have user controls that you can't see on design mode, and I have forms with multiple inheritance that I can't change to user control, and I have forms that I design to work as a form and as a control. So, some forms are not too "easy" to change the parent class. I mentioned that I want to design a tabbed application, in mi environment, same form must be opened on tab (you can design a user control to do that), but in other situation I want to open form as a ShowDialog form, in that case, you can't show UserControl as a form, and I don't go to create one form just to show it.
GeneralRe: UserControl Pin
Brad Bruce24-Jun-12 8:56
Brad Bruce24-Jun-12 8:56 
GeneralMy vote of 5 Pin
Carlos190722-Jun-12 0:32
professionalCarlos190722-Jun-12 0:32 

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.