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

UserControls as SDI Windows

0.00/5 (No votes)
15 Jan 2014 1  

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 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