using System; using System.Windows.Forms; public class Form1 : Form { private TabControl tabControl; public Form1() { tabControl = new TabControl(); tabControl.Dock = DockStyle.Fill; TabPage tabPage = new TabPage("Tab 1"); tabControl.TabPages.Add(tabPage); this.Controls.Add(tabControl); Button button = new Button { Text = "Open Form2" }; button.Click += (s, e) => OpenForm2(); tabPage.Controls.Add(button); } private void OpenForm2() { Form2 form2 = new Form2(); form2.TopLevel = false; // 使 Form2 成為一個子控制項 form2.FormBorderStyle = FormBorderStyle.FixedToolWindow; // 設定邊框樣式 form2.Dock = DockStyle.Fill; // 停靠到 TabPage tabControl.SelectedTab.Controls.Add(form2); form2.Show(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }
using System.Windows.Forms; public class Form2 : Form { public Form2() { this.Text = "Form2"; this.Size = new System.Drawing.Size(300, 200); } }
using System; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; public class Form1 : Form { private DockPanel dockPanel; private TabControl tabControl; public Form1() { dockPanel = new DockPanel(); dockPanel.Dock = DockStyle.Fill; this.Controls.Add(dockPanel); tabControl = new TabControl(); tabControl.Dock = DockStyle.Top; tabControl.Height = 100; this.Controls.Add(tabControl); TabPage tabPage = new TabPage("Tab 1"); tabControl.TabPages.Add(tabPage); Button button = new Button { Text = "Open Form2" }; button.Click += (s, e) => OpenForm2(); tabPage.Controls.Add(button); } private void OpenForm2() { Form2 form2 = new Form2(); form2.Show(dockPanel); // 使用 DockPanel 顯示 Form2 } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }
using System; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; public class Form2 : DockContent { public Form2() { this.Text = "Form2"; this.Size = new System.Drawing.Size(300, 200); } }