Click here to Skip to main content
11,920,156 members (57,786 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Forms
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 25-Jul-12 13:26pm
Edited 25-Jul-12 13:52pm
Sergey Alexandrovich Kryukov 25-Jul-12 18:53pm
Pretty good idea, my 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^],[^].

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:[^],[^].


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

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

Good luck,
Member 7802968 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. ;(
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.
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...
Maciej Los 27-Mar-13 10:01am
As i wrote in other comment: Your suggestions are very useful ;)
Maciej Los 15-Dec-13 13:49pm
Don't worry about it. I would say: it happens sometimes...
Thank you, Sergey :)
Sergey Alexandrovich Kryukov 27-Mar-13 10:08am
Thank you for the new link I just added.
Those were only suggestions as I did not try it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This might be of help to you: FireFox-like Tab Control[^]
Good idea and the link, my 5.
Maciej Los 27-Mar-13 10:01am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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)

  Print Answers RSS
0 OriginalGriff 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 15 Dec 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100