如何在C#中添加和删除“自定义”选项卡

我正在创建一个需要添加或删除选项卡(选项卡控件)的应用程序。 我已经完成了标签的添加和删除,但我有自定义按钮而不是使用标签。 (单击此按钮将转到第一个标签页):

//This will make it go to TAB 1 private void button1_Click(object sender, EventArgs e) { tabControl1.SelectedIndex = 0; } //This will change the MOUSEENTER to the correct image private void button1_MouseEnter(object sender, EventArgs e) { button1.MouseEnter += new EventHandler(button1_MouseEnter); this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Tab_Down)); } //This will change the MOUSELEAVE to the correct image private void button1_MouseLeave(object sender, EventArgs e) { button1.MouseLeave += new EventHandler(button1_MouseLeave); this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Tab_Norm)); } 

但是,如何创建它,以便当您单击“添加选项卡”按钮时,它会创建一个新选项卡,但它也会创建一个带有tabControl1.SelectedIndex = 1;的新按钮tabControl1.SelectedIndex = 1; 以它为例。

编辑

我这样做是为了添加一个新选项卡(到tabControl1):

  private void button2_Click(object sender, EventArgs e) { string title = "TabPage " + (tabControl1.TabCount + 1).ToString(); TabPage myTabPage = new TabPage(title); tabControl1.TabPages.Add(myTabPage); } 

这会在现有的选项卡上添加一个新选项卡。 但是我该怎么做才能创建一个带有上面按钮属性的新按钮,但不是tabControl1.SelectedIndex = 1; 它确实tabControl1.SelectedIndex = 3; 每次添加新标签时都会上升? – 谢谢

经过一些更新后,这是我的答案的新版本。 它告诉你如何

  • 添加一个Button ,添加一个新的TabPages并选择它
  • 使Tab控件在每个选项卡的右侧绘制关闭’x’字符,如Firefox或Visual Studio。

按你想要的方式添加Button ,可能是Text =“+”,高度就像标签的高度一样。 它自动定位在选项卡上。 如果您的Tab被移动或resize,您可能需要重新定位它。

Form_Load事件中, Tab被设置为OwnerDrawFixed并且每个页面的文本都附加了几个空格,以便为结束x腾出空间。 代码创建一个类变量closeX来保存当前的x-rectangle并在MouseClick事件中对其进行测试。

确保连接tabControl1_DrawItemtabControl1_MouseClick事件!

  private void cb_addPage_Click(object sender, EventArgs e) { string title = "TabPage " + (tabControl1.TabCount + 1).ToString() + " "; TabPage myTabPage = new TabPage(title); tabControl1.TabPages.Add(myTabPage); tabControl1.SelectedTab = myTabPage; } private void Form1_Load(object sender, EventArgs e) { tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; cb_addPage.Top = tabControl1.Top; cb_addPage.Left = tabControl1.Right - cb_addPage.Width; foreach (TabPage tp in tabControl1.TabPages) tp.Text += " "; } Rectangle closeX = Rectangle.Empty; private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) { Size xSize = new Size(13,13); TabPage tp = tabControl3.TabPages[e.Index]; e.DrawBackground(); using (SolidBrush brush = new SolidBrush(e.ForeColor) ) e.Graphics.DrawString(tp.Text+ " ", e.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 4 ); if (e.State == DrawItemState.Selected) { closeX = new Rectangle( e.Bounds.Right - xSize.Width, e.Bounds.Top, xSize.Width, xSize.Height); using (SolidBrush brush = new SolidBrush(Color.LightGray)) e.Graphics.DrawString("x", e.Font, brush, e.Bounds.Right - xSize.Width, e.Bounds.Y + 4); } } private void tabControl1_MouseClick(object sender, MouseEventArgs e) { if (closeX.Contains(e.Location)) tabControl1.TabPages.Remove(tabControl1.SelectedTab); } 

如果要使用图像来装饰选项卡,请在表单中包含ImageList,使用图像加载它,如果关闭按钮是第1张图像,请更改以下代码:

  if (e.State == DrawItemState.Selected) { closeX = new Rectangle(e.Bounds.Right - xSize.Width - 3, e.Bounds.Top + 5, xSize.Width, xSize.Height); e.Graphics.DrawImage(imageList1.Images[0], closeX, new Rectangle(0,0,16,16), GraphicsUnit.Pixel ); } 

我附上了几页的Tab截图

所有者绘制选项卡与关闭按钮

一个人用这个 在此处输入图像描述 关闭按钮图像:

在此处输入图像描述

更新:请注意,如果您的TabPage包含IDisposable对象,您应确保在删除页面之前将它们全部处理掉! 看这里的代码示例!