创建一堆重叠的面板是设计时的噩梦.
我建议在单独的选项卡上使用每个"视图"的选项卡控件,然后在运行时选择正确的选项卡.您可以通过在表单的Load事件中放置类似的内容来避免显示选项卡标题:
tabControl1.Top = tabControl1.Top - tabControl1.ItemSize.Height; tabControl1.Height = tabControl1.Height + tabControl1.ItemSize.Height; tabControl1.Region = new Region(new RectangleF(tabPage1.Left, tabPage1.Top, tabPage1.Width, tabPage1.Height + tabControl1.ItemSize.Height));
我所做的是建立一个面板,您的不同视图将位于主窗体上.然后为您的不同视图创建用户控件.
然后当我想在'view'之间切换时,你将它停靠在主窗体上的Panel ..代码看起来有点像这样.
我喜欢这个,因为你可以重复使用你的视图,就像你想在选项卡中打开一个视图,你可以将用户控件停靠在标签页中..甚至从tabpage而不是usercontrol继承,使事情变得更加通用
public partial class MainForm : Form { public enum FormViews { A, B } private MyViewA viewA; //user control with view a on it private MyViewB viewB; //user control with view b on it private FormViews _formView; public FormViews FormView { get { return _formView; } set { _formView = value; OnFormViewChanged(_formView); } } protected virtual void OnFormViewChanged(FormViews view) { //contentPanel is just a System.Windows.Forms.Panel docked to fill the form switch (view) { case FormViews.A: if (viewA != null) viewA = new MyViewA(); //extension method, you could use a static function. this.contentPanel.DockControl(viewA); break; case FormViews.B: if (viewB != null) viewB = new MyViewB(); this.contentPanel.DockControl(viewB); break; } } public MainForm() { InitializeComponent(); FormView = FormViews.A; //simply change views like this } } public static class PanelExtensions { public static void DockControl(this Panel thisControl, Control controlToDock) { thisControl.Controls.Clear(); thisControl.Controls.Add(controlToDock); controlToDock.Dock = DockStyle.Fill; } }