Click here to Skip to main content
12,623,027 members (29,027 online)
Click here to Skip to main content
Add your own
alternative version

Stats

40.1K views
9 bookmarked
Posted

Use form as subform

, 21 Jun 2012 CPOL
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
BillWoodruff16-Dec-14 23:13
professionalBillWoodruff16-Dec-14 23:13 
GeneralRe: My vote of 1 Pin
Antonio Lopez R30-Dec-14 17:48
memberAntonio Lopez R30-Dec-14 17:48 
Question limits of this way Pin
oula alsheikh16-Dec-14 4:09
memberoula alsheikh16-Dec-14 4:09 
AnswerRe: limits of this way Pin
Antonio Lopez R30-Dec-14 17:51
memberAntonio Lopez R30-Dec-14 17:51 
GeneralThis is exactly what I needed! Pin
_onTy_5-Feb-14 21:08
member_onTy_5-Feb-14 21:08 
QuestionUserControl Pin
Brad Bruce23-Jun-12 8:21
memberBrad Bruce23-Jun-12 8:21 
AnswerRe: UserControl Pin
Antonio Lopez R23-Jun-12 20:31
memberAntonio Lopez R23-Jun-12 20:31 
GeneralRe: UserControl Pin
Brad Bruce24-Jun-12 9:56
memberBrad Bruce24-Jun-12 9:56 
GeneralMy vote of 5 Pin
Carlos190722-Jun-12 1:32
memberCarlos190722-Jun-12 1: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 21 Jun 2012
Article Copyright 2012 by Antonio Lopez R
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid