tabControl中的关闭按钮

是否有人可以告诉我如何在C#中使用tabControl在每个选项卡中添加关闭按钮? 我计划使用按钮pic替换我的标签中的[x] ..

谢谢

在没有派生类的情况下,这里有一个简洁的片段: http : //www.dotnetthoughts.net/implementing-close-button-in-tab-pages/

将Tab控件的DrawMode属性设置为OwnerDrawFixed。 此属性决定系统或开发人员是否绘制字幕。 在Tab控件的DrawItem事件中添加代码 – 将调用此事件来绘制每个选项卡页面。

//This code will render a "x" mark at the end of the Tab caption. e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 15, e.Bounds.Top + 4); e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 4); e.DrawFocusRectangle(); 

现在,对于关闭按钮操作,我们需要将以下代码添加到Tab控件的MouseDown事件中。

 //Looping through the controls. for (int i = 0; i < this.tabControl1.TabPages.Count; i++) { Rectangle r = tabControl1.GetTabRect(i); //Getting the position of the "x" mark. Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 9, 7); if (closeButton.Contains(e.Location)) { if (MessageBox.Show("Would you like to Close this Tab?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.tabControl1.TabPages.RemoveAt(i); break; } } } 

添加到其他答案…当我们可以使用.SelectedIndex和.SelectedTab检测当前选项卡时,为什么要遍历鼠标单击事件的所有选项卡?

像这样:

 private void tabControl1_MouseDown(object sender, MouseEventArgs e) { Rectangle r = tabControl1.GetTabRect(this.tabControl1.SelectedIndex); Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 9, 7); if (closeButton.Contains(e.Location)) { this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab); } } 

似乎发生的情况是,当您单击tabPage关闭它时,它也会被选中,从而允许关闭按钮关闭右侧tabPage。 对我来说它有效,但请特别小心,因为我不能完全确定可能的缺点(我的初始句子不是一个完全修辞的问题,因为我对.Net有点新鲜……)。

试试这段代码:

  private Point _imageLocation = new Point(13, 5); private Point _imgHitArea = new Point(13, 2); private void Form1_Load(object sender, EventArgs e) { tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; tabControl1.DrawItem += tabControl1_DrawItem; CloseImage = WindowsFormsApplication3.Properties.Resources.closeR; tabControl1.Padding = new Point(10, 3); } private void TabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { try { Image img = new Bitmap(CloseImage); Rectangle r = e.Bounds; r = this.tabControl1.GetTabRect(e.Index); r.Offset(2, 2); Brush TitleBrush = new SolidBrush(Color.Black); Font f = this.Font; string title = this.tabControl1.TabPages[e.Index].Text; e.Graphics.DrawString(title, f, TitleBrush, new PointF(rX, rY)); if (tabControl1.SelectedIndex >= 1) { e.Graphics.DrawImage(img, new Point(rX + (this.tabControl1.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y)); } } catch (Exception) { } } private void TabControl1_Mouse_Click(object sender, System.Windows.Forms.DrawItemEventArgs e) { TabControl tc = (TabControl)sender; Point p = e.Location; int _tabWidth = 0; _tabWidth = this.tabControl1.GetTabRect(tc.SelectedIndex).Width - (_imgHitArea.X); Rectangle r = this.tabControl1.GetTabRect(tc.SelectedIndex); r.Offset(_tabWidth, _imgHitArea.Y); r.Width = 16; r.Height = 16; if (tabControl1.SelectedIndex >= 1) { if (r.Contains(p)) { TabPage TabP = (TabPage)tc.TabPages[tc.SelectedIndex]; tc.TabPages.Remove(TabP); } } } 

看看这段代码