在TabControl的右上角放置一个关闭按钮 [英] Putting a Close button on top right of TabControl

查看:120
本文介绍了在TabControl的右上角放置一个关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS2008 WinForms中的TabControl不包括右上角的常规关闭选项卡小x。有没有办法轻松添加右上角的小x,以便点击它可以触发一个事件,我可以编码删除当前活动的选项卡?

The TabControl in VS2008 WinForms does not include the normal "close tab" small x in the top right corner. Is there a way to easily add the small x in the top right corner so that clicking it can trigger an event that I can code to remove the currently active tab?

推荐答案

我从来没有尝试过这个功能(听起来像个好主意,所以我对这个问题进行了投票,很少发生这种情况),但我很确定它不会太难。最有可能的是,您将能够在从 TabControl OnPaint 中呈现此类按钮的图像。 c>或 Paint 事件处理程序。您需要为此按钮引入状态,并根据当前状态对其进行不同的渲染。要反映渲染中的状态更改,您需要调用方法 Invalidatew ;您可以将它与 Rectanlge Region 参数一起使用,以使控件的一部分无效,其中按钮被渲染。要处理点击,您需要处理控件的事件 MouseDown MouseUp ,或者更好地覆盖虚拟方法 OnMouseDown OnMouseUp



请请参阅:

http://msdn.microsoft。 com / en-us / library / system.windows.forms.tabcontrol.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx [ ^ ]。



这是轻量级方法,你不为按钮创建一个单独的控件但是实现按钮式选项卡控件本身的渲染表面部分的行为。



替代方法(总是有效)是使用一些可用的按钮控件或开发一个一个单独的控件,并使用可用的选项卡控件和按钮控件作为childen在复合控件中的选项卡控件上使用它,您可以将其作为 UserControl 进行开发。如果您不需要使用Designer进行此类控制(我强烈建议您尽可能避免使用Designer),您可以直接从类 ContainerControl中派生复合控件并在手动编写的代码中插入两个子代码 - 在很多情况下,这更容易,并提供更好的代码质量。请参阅:

http://msdn.microsoft .com / zh-cn / library / system.windows.forms.containercontrol.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx [ ^ ]。







请参阅此CodeProject文章:具有标签页关闭功能的TabControl [ ^ ]。



(感谢Maciej Los指出了这篇文章。)



祝你好运,

-SA
I never tried this feature (sound like a good idea, so I up-voted the question, which happens very rarely), but I'm pretty sure it won't be too hard. Most likely, you will be able to render the image of such button in the overridden method OnPaint in the control class derived from TabControl or in the Paint event handler. You will need to introduce states for this button and render it differently depending on the current state. To reflect the state change in rendering, you will need to call the method Invalidatew; you can use it with Rectanlge or Region parameter, to invalidate only the part of the control, where the button is rendered. To handle the click, you will need to handle the events MouseDown and MouseUp of the control, or, better, override the virtual methods OnMouseDown and OnMouseUp.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

That was the light-weight approach, where you don't create a separate control for a button but implement button-like behavior for the part of the rendered surface of the tab control itself.

The alternative approach (which will always work) is to use some available button control or develop one as a separate control and use it on top of your tab control in a composite control using available tab control and your button control as childen, which you can develop as a UserControl. If you don't need to use the Designer for such control (I highly recommend to avoid using the Designer when it is possible and practical enough), you can derive your composite control directly from the class ContainerControl and insert two children in your manually written code — in many cases, this is easier and gives your better code quality. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx[^].



Please see this CodeProject article: A TabControl with tab page closing capability[^].

(Thanks to Maciej Los who pointed out this article.)

Good luck,
—SA


这可能对您有所帮助:< a href =http://www.codeproject.com/Articles/20050/FireFox-like-Tab-Control> FireFox-like Tab Control [ ^ ]
This might be of help to you: FireFox-like Tab Control[^]


另一个解决方案:具有标签页关闭功能的TabControl [ ^ ]
And another solution: A TabControl with tab page closing capability[^]


这篇关于在TabControl的右上角放置一个关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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