gpt4 book ai didi

c# - tabControl 中的关闭按钮

转载 作者:IT王子 更新时间:2023-10-29 04:13:54 25 4
gpt4 key购买 nike

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

谢谢

最佳答案

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

将选项卡控件的 DrawMode 属性设置为 OwnerDrawFixed。此属性决定系统或开发人员是否可以绘制字幕。在选项卡控件的 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();

现在对于关闭按钮操作,我们需要将以下代码添加到选项卡控件的 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;
}
}
}

关于c# - tabControl 中的关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3183352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com