Click here to Skip to main content
15,881,789 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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?
Posted
Updated 25-Jul-12 12:52pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Jul-12 18:53pm    
Pretty good idea, my 5.
--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[^].

[EDIT]

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

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

Good luck,
—SA
 
Share this answer
 
v5
Comments
[no name] 25-Jul-12 19:31pm    
Thanks - While I was hoping for something really simple, the second alternative you mention sounds like something I could try without too much work. I'll give it a shot.
Maciej Los 15-Dec-13 7:44am    
Sergey, i don't know when you had added a link to the CP article, but it looks like someone down-voted my answer, because it refer to the same CP article. ;(
Sergey Alexandrovich Kryukov 26-Jul-12 1:25am    
You are welcome. Both ways are not too hard, but in first ways there could be some delicate detail.
If you agree it makes sense, consider accepting the answer formally (green button) -- thanks.
--SA
Sergey Alexandrovich Kryukov 15-Dec-13 12:51pm    
That could be a reason. I think I added this like long time ago but later than your answer posts, as I said so in my "thank you" words above. Now I added my vote of 5 and a credit in my answer, but I can see two votes of one now. This is unpleasant; sorry about this little trouble. I should have done it right in first place. My short comment might not be visible enough...
—SA
Maciej Los 27-Mar-13 10:01am    
As i wrote in other comment: Your suggestions are very useful ;)
+5!
This might be of help to you: FireFox-like Tab Control[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jul-12 1:26am    
Good idea and the link, my 5.
--SA
Maciej Los 27-Mar-13 10:01am    
+5!
 
Share this answer
 
Comments
Maciej Los 15-Dec-13 7:41am    
Who's down-voted?
CHill60 15-Dec-13 13:03pm    
Don't know - been a lot of that going on recently. I've seen SA's comments above so my +5 too to help balance it out a bit
Maciej Los 15-Dec-13 13:47pm    
Thank you, CHill ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900