Click here to Skip to main content
Rate this: bad
good
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
v2
Comments
Sergey Alexandrovich Kryukov at 25-Jul-12 18:53pm
   
Pretty good idea, my 5.
--SA
Rate this: bad
good
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:
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
  Permalink  
v5
Comments
Member 7802968 at 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 at 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 at 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 at 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 at 27-Mar-13 10:01am
   
As i wrote in other comment: Your suggestions are very useful ;)
+5!
Maciej Los at 15-Dec-13 13:49pm
   
Don't worry about it. I would say: it happens sometimes...
Thank you, Sergey :)
Sergey Alexandrovich Kryukov at 27-Mar-13 10:08am
   
Thank you for the new link I just added.
Those were only suggestions as I did not try it.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This might be of help to you: FireFox-like Tab Control[^]
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 26-Jul-12 1:26am
   
Good idea and the link, my 5.
--SA
Maciej Los at 27-Mar-13 10:01am
   
+5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
Maciej Los at 15-Dec-13 7:41am
   
Who's down-voted?
CHill60 at 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 at 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 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 15 Dec 2013
Copyright © CodeProject, 1999-2014
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