65.9K
CodeProject 正在变化。 阅读更多。
Home

用户控件作为 SDI 窗口

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (3投票s)

2014 年 1 月 15 日

CPOL
viewsIcon

8948

downloadIcon

234

介绍 

本技巧提供了一个示例,其中单文档界面应用程序的内容区域在两个或多个 UserControl 之间切换。

背景

本技巧是针对 快速解答 中的一个问题而创建的。

使用代码

代码包含一个父 Form,其中包含一个 ToolStripContainer。工具栏包含两个按钮,用于在用户控件之间切换。用户控件根据按钮按下事件添加到/从容器的 Content 属性中添加/移除。

using System.Windows.Forms;

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

        /// 
        /// Initializes a new instance of the  class.
        /// 
        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);
        }
    }
}
© . All rights reserved.