Results 1 to 3 of 3

Thread: TabControl with Close Button and Text Align

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    39

    TabControl with Close Button and Text Align

    One major flaw in the Tab Control in VB is the fact that it doesn't have a close button. Another issue is the alignment of the text. Usually these two problems can't be solved without manually drawing them on and changing the DrawMode to OwnerDrawFixed. And even once you do that, the tabs usually end up looking terrible.

    Here's a way that I found to get a close button on tabs and also align the text however you want!

    This is a more advanced version of the following code. I added the text fix and fixed a few minor issues with what he already had:
    http://www.vbforums.com/showthread.php?t=622242

    How to use:
    1. Add a new class to your application (preferably named TabControlEx.vb)
    2. Copy and paste this code into that class.
    3. Add the attached image to resources. You can use your own, but I really like the way this one looks.
    3. Rebuild.
    4. Now you should see a new item in the Control Toolbox named TabControlEx. Drag this control to your form.

    New Features:
    1. The ShowCloseButtonOnTabs property controls whether close button is shown on the tabs on not. By default this is True.
    2. The CloseButtonClick event is raised when any of the close buttons is clicked. By default this closes the tab, but you can set the e.Cancel = True inside this event handler to prevent closing the tab.
    3. Text on tabs is aligned the way you want it. Changed the text/font the way you normally would (TabControl.text = w/e) and it will automatically appear properly.

    Note:
    -The width of the tab I tested it with was 115
    -Change occurrences of the following to change the way the text is aligned:
    TabName.Size = New Size(rect.Width - btn.Width - 10, rect.Height)
    TabName.Location = New Point(rect.X + 10, rect.Y + 1)

    **** The 'X' Does Not Show Up On the First Tab When It's The Only One Open!****
    **** I did this on purpose****
    You can fix this very easily by taking out the if statement that sets it's visibility to false
    in the functioncalled RePositionCloseButtons. But you will need to add another if
    statement in the function OnCloseButtonClick that will close the form if the TabCount -1
    is 0. This is to prevent crashing. I just thought it was a neat feature to not show the 'X'.
    It's the same way in FireFox.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Zombie666; May 20th, 2011 at 01:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width