Click here to Skip to main content
11,707,254 members (64,276 online)
Click here to Skip to main content

UserControls as SDI Windows

, 15 Jan 2014 CPOL 3.4K 119 2
Rate this:
Please Sign up or sign in to vote.

Introduction 

This tip provides an example in which the content area of a single document interface application is switched between two or more UserControls.

Background

This tip was created in response to a question in Quick Answers.

Using the code

The code contains a parent Form, containing a ToolStripContainer. The tool strip contains two buttons, used to switch between user controls. The user controls are added/removed to the container's Content property in response to button presses.

using System.Windows.Forms;

namespace RedCell.App.Example.UserControls
{
    /// <summary>
    /// The application's main form.
    /// </summary>
    public partial class MainForm : Form
    {
        private readonly UserControl _christmasCarolControl;
        private readonly UserControl _greatExpectationsControl;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"> class.
        /// </see></summary>
        public MainForm()
        {
            InitializeComponent();

            // Create a single instance of each child control.
            _christmasCarolControl = new ChristmasCarol {Dock = DockStyle.Fill};
            _greatExpectationsControl = new GreatExpectations { Dock = DockStyle.Fill };
        }

        private void GreatExpectationsButton_Click(object sender, System.EventArgs e)
        {
            ToolStripContainer.ContentPanel.Controls.Clear();
            ToolStripContainer.ContentPanel.Controls.Add(_greatExpectationsControl);
        }

        private void ChristmasCarolButton_Click(object sender, System.EventArgs e)
        {
            ToolStripContainer.ContentPanel.Controls.Clear();
            ToolStripContainer.ContentPanel.Controls.Add(_christmasCarolControl);
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Yvan Rodrigues
Systems Engineer Clearpath Robotics
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the industry. He is Senior Systems Developer at Clearpath Robotics

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.

Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 15 Jan 2014
Article Copyright 2014 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid