在标签页标题中添加关闭按钮 [英] Add Close button in Tab Pages Header

查看:126
本文介绍了在标签页标题中添加关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的项目中有一个标签控件...

现在,我想在我的项目中添加关闭按钮每个标签页头......

如何实现它?

解决方案

假设这是Windows Forms。



MS TabControl的选择非常有限:如果你想要任何类型的Tab-Header自定义,你必须使控件OwnerDrawn,并实现自定义绘画。并且,如果你想在Tab-Headers中进行命中检测,你必须实现它。



也就是说,你可以通过自定义Tab找到很好的OwnerDrawn TabControls示例-Headers,比如Ravi在他的帖子中提到你的CP上的那个。



这是我几年前在切换到使用之前写的另一种选择第3个。派对TabControl(在Lidor IntegralUI套件中)。



这个替代方案使用ContextMenu,对于这个例子......我将限制为单个菜单-entry:关闭。



1.您可能希望将TabControl SizeMode设置为'Fixed,并设置'ItemSize参数以使Tab-Headers成为你想要的大小。



2.在表格上放一个ContextMenuStrip,创建一个单独的菜单项,文字为关闭。



3.设置TabControl以使用ContextMenuStrip。



4.双击关闭菜单项以生成一个Click ToolStripMenuItem的EventHandler,并将其编码为:

  private   void  closeToolStripMenuItem_Click ( object  sender,EventArgs e)
{
Point mouseAt = MousePosition;

foreach (TabPage theTabPage in tabControl1.TabPages)
{
if (tabControl1.RectangleToScreen(tabControl1.GetTabRect(theTabPage.TabIndex))。包含(mouseAt))
{
// 仅供测试
Console.WriteLine(theTabPage.Text + Environment.NewLine);

// 这是您编写代码以删除TabPage的地方来自TabControl的TabPages集合的
//
// 唯一可以模拟关闭它的方法

断裂;
}
}
}

这里发生了什么:



1.单击ToolStripMenuItem时记录屏幕坐标中的鼠标位置。



2. TabControl中的TabPages被迭代,并且Tab-Header边界被转换为屏幕co-纵坐标。



3.对已翻译的Tab-Header边界进行测试,看它们是否包含鼠标所在的点。



4.如果检测到命中:出于测试目的:将一条消息写入控制台以显示该TabPage的文本,并停止迭代。



免责声明:我暂时没有使用过这段代码,你应该自己彻底测试命中检测。


如果你使用的是WinForms,请参阅< a href =http://www.codeproject.com/Articles/20050/FireFox-like-Tab-Control> this [ ^ ] CP文章。



/ ravi


Hi,

I have a tab control in my project...
Now, I want to add close buttons in my each of the tabpages header...
How can I achieve it ?

解决方案

Assuming this is Windows Forms.

The choices you have with the MS TabControl are very limited: if you want any type of customization of the Tab-Headers, you must make the control OwnerDrawn, and implement custom painting. And, if you want hit-detection inside the Tab-Headers, you must implement that.

That said, you can find really nice examples of OwnerDrawn TabControls with custom Tab-Headers, such as the one here on CP that Ravi referred you to in his post.

Here's an alternative I wrote a few years ago before I switched over to using a 3rd. party TabControl (in the Lidor IntegralUI Suite).

This alternative uses a ContextMenu, which ... for this example ... I'll limit to a single menu-entry: "Close."

1. you may wish to set the TabControl SizeMode to 'Fixed, and set the 'ItemSize parameters to make the Tab-Headers the size you wish.

2. drop a ContextMenuStrip on the Form, create a single menu-item with the text "Close."

3. set the TabControl to use the ContextMenuStrip.

4. double-click on the "Close" menu item to generate a Click EventHandler for the ToolStripMenuItem, and code it like this:

private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
    Point mouseAt = MousePosition;

    foreach (TabPage theTabPage in tabControl1.TabPages)
    {
        if (tabControl1.RectangleToScreen(tabControl1.GetTabRect(theTabPage.TabIndex)).Contains(mouseAt))
        {
            // for testing only
            Console.WriteLine(theTabPage.Text + Environment.NewLine);

            // this is where you'd write code to remove the TabPage
            // from the TabControl's TabPages collection which is
            // the only way you can simulate "closing" it

            break;
        }
    }
}

What's going on here:

1. when the ToolStripMenuItem is Clicked the mouse position in screen co-ordinates is recorded.

2. the TabPages in the TabControl are iterated over, and the Tab-Header bounds are translated into screen co-ordinates.

3. the translated Tab-Header bounds are tested to see if they contain the point the mouse went down at.

4. if the hit is detected: for testing purposes: a message is written to the Console to display the Text of that TabPage, and the iteration is halted.

Disclaimer: I haven't used this code in a while, and you should thoroughly test the hit-detection yourself.


If you're using WinForms, see this[^] CP article.

/ravi


这篇关于在标签页标题中添加关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆