Click here to Skip to main content
Click here to Skip to main content

Use form as subform

, 21 Jun 2012
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Antonio Lopez R
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.
 
I like punk and heavy metal, I'm a chinese boxing practitioner and amateur sanshou fighter.
Follow on   Twitter

Comments and Discussions

 
GeneralThis is exactly what I needed! Pinmember_onTy_5-Feb-14 20:08 
QuestionUserControl PinmemberBrad Bruce23-Jun-12 7:21 
AnswerRe: UserControl PinmemberAntonio Lopez R23-Jun-12 19:31 
GeneralRe: UserControl PinmemberBrad Bruce24-Jun-12 8:56 
GeneralMy vote of 5 [modified] PinmemberCarlos190722-Jun-12 0:32 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 21 Jun 2012
Article Copyright 2012 by Antonio Lopez R
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid