自定义的TabControl的各个选项卡闭幕 [英] Customizing a TabControl for the Closing of Individual Tabs

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

问题描述

我的情况是这样的:

我工作在C#WinForms应用程序有一个TabControl,将产生另一个Tab页每次被点击时的主要页面内的按钮。每一个新的TabPage将包含用户控件定义的布局。

我的问题是:


  1. 我怎样才能让用户然后关闭了在运行时动态创建的标签之一?


  2. 我怎么可能去有关修改TabControl的本身,以便它在用户可能以关闭该特定选项卡单击每个选项卡的小X? (如Firefox有)


  3. 我怎样才能的TabControl的SelectedIndex属性暴露给用户的控制,如果我想用一个按钮关闭用户控制里面的标签呢?



解决方案

我创造了约一年前派生标签控件。我不打算在这里发表的来源,因为它是关于700线长,codeD相当混乱。也许我会找一些时间来清理code起来,然后在这里释放。现在我将简单介绍一下它的方式是建立

每个标签页都有到的留下了一个X图标的标题和标签页中通过拖放和多标签控制的移动支持他们重新排序。

我选择简单的方法来对标签页的图标。该标签控件具有<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.imagelist.aspx\"><$c$c>TabControl.ImageList属性和标签页有一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.tabpage.imageindex.aspx\"><$c$c>TabPage.ImageIndex属性。所以,我只是增加了三个图标添加到图像列表 - 正常,悬停,pressed - 和处理鼠标事件

通过<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.gettabrect.aspx\"><$c$c>TabControl.GetTabRect()你可以测试鼠标在一个特定的标签页,并与一些数学你发现,如果它是在图标上。然后你只需要更改图标依赖于鼠标按钮的状态,并最终鼠标下删除标签页按钮是否是pressed。

这个解决方案的主要问题是,在计算如果鼠标在图标上,需要知道在哪里的图标相对于标签页画,这可能与新的Windows版本中改变。和图标是标题的左边,但看起来并不太坏。

My scenario is the following:

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.

My Questions are:

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

  2. 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)

  3. 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?

解决方案

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.

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.

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.

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.

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天全站免登陆