自定义用于关闭单个选项卡的 TabControl [英] Customizing a TabControl for the Closing of Individual Tabs

查看:29
本文介绍了自定义用于关闭单个选项卡的 TabControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 开发一个 winforms 应用程序,该应用程序在 tabcontrol 的主页内有一个按钮,每次单击时都会生成另一个 tabpage.每个新标签页将包含一个由用户控件定义的布局.

I am working on a winforms application in C# that has a button inside the main page of a tabcontrol that will generate another tabpage each time that it is clicked. Each new tabpage will contain a layout defined by a user control.

  1. 我如何允许用户关闭在运行时动态创建的选项卡之一?

  1. How can I allow the user to then close one of the tabs that were created dynamically at runtime?

我该如何修改 tabcontrol 本身,使其在每个选项卡中都有一个小X",用户可以单击该X"以关闭该特定选项卡?(就像 Firefox 一样)

How might I go about modifying the tabcontrol itself so that it has a small 'X' in each tab that the user may click on in order to close that particular tab? (Like Firefox has)

如果我想使用用户控件内的按钮关闭选项卡,如何将选项卡控件的 SelectedIndex 属性公开给用户控件?

How can I expose the SelectedIndex property of the tabcontrol to the user control if I want to close the tab with a button inside the user control instead?

推荐答案

我大约在一年前创建了一个派生的选项卡控件.我不打算在这里发布源代码,因为它大约有 700 行长并且编码非常混乱.也许我会找一些时间清理代码,然后在这里发布.现在我将简要概述它的构建方式.

I created a derived tab control about one year ago. I am not going to post the source here, because it's about 700 lines long and coded quite messy. Maybe I will find some time to clean the code up and then release it here. For now I will briefly outline the way it is build.

每个标签页标题的左侧都有一个X"图标,标签页支持通过拖放和在多个标签控件之间移动来重新排序.

Each tab page has a 'X' icon to the left of the title and the tab pages support reordering by drag and drop and moving them between multiple tab control.

我选择了在标签页上获取图标的简单方法.选项卡控件具有 TabControl.ImageList 属性和标签页具有 TabPage.ImageIndex 属性.所以我只是在图像列表中添加了三个图标 - 正常、悬停、按下 - 并处理鼠标事件.

I choose the easy way to get the icon on the tab pages. The tab control has the TabControl.ImageList property and a tab page has a TabPage.ImageIndex property. So I just added three icons to a image list - normal, hover, pressed - and process the mouse events.

使用 TabControl.GetTabRect() 你可以测试鼠标是否在特定的标签页上,并通过一些数学计算你会发现它是否在图标上.然后你只需要根据鼠标按钮的状态改变图标,如果按钮被按下,最终删除鼠标下的标签页.

With TabControl.GetTabRect() you can test if the mouse is over a specific tab pages and with some math you find if it is over the icon. Then you just need to change the icon depending on the mouse button state and eventually remove the tab page under the mouse if the button was pressed.

这个解决方案的主要问题是,计算鼠标是否在图标上需要知道图标相对于标签页的绘制位置,这可能会随着新的 Windows 版本而改变.图标在标题的左侧,但看起来还不错.

The main problem with this solution is, that calculating if the mouse is over the icon requires to know where the icon is painted relative to the tab page and this might change with a new windows version. And the icon is to the left of the title, but that does not look too bad.

这篇关于自定义用于关闭单个选项卡的 TabControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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