Results 1 to 5 of 5

Thread: [UPDATE] Custom TabControl incl Designer, Tab-specific ContextMenus and Tab-Dragging

  1. #1

    Thread Starter
    Join Date
    Apr 2007
    The Netherlands

    [UPDATE] Custom TabControl incl Designer, Tab-specific ContextMenus and Tab-Dragging


    UPDATE: No longer uses a tabcontrol in a tabcontrol-usercontrol. Just one custom tabcontrol, one designer and one custom tabpage control.



    Here is a TabControl (with custom(izable) TabPages) that includes the following:
    • Standard TabControl look and feel

    No ugly custom drawing. But ofcourse, if you want to have custom drawing, there is nothing that stops you!

    • Design-time support

    It is possible to switch, add and remove TabPages in the Designer like you can with the normal TabControl.

    • Tab-specific ContextMenuStrips

    Rightclicking a TabPage will bring up a ContextMenuStrip of your choice. The ContextMenuStrip is a property of the TabPage, so you can have a different (or none) ContextMenuStrip for each TabPage.

    • Draggable Tabs (even from one TabControl to the other)

    Drag tabs with your mouse to change the order of the tabs, or even drag them from one of my custom TabControls to another.

    • ... Possibly more when I finish it!

    A lot of work went into getting the Design-time support to work. Usually, when you Inherit from the TabControl, you get the Design-time support automatically. However, (I think) if you want to use custom TabPages, you need to create your own Designer. I don't know if it was really necessary to do it the way I did, but after trying hundreds of things I felt I was forced to make my own Custom Designer.

    So in addition to being a 'normal' TabControl with enhanced functionality, this is also an example of a pretty complicated custom Designer.

    Credits to Mick Doherty for his TabControl Tips. This is where I got much of the Designer class from, thanks!

    Here is a short explanation of the various classes in my example:

    The main TabControl is the cTabControl.
    If you open the code of the cTabControl class, you can see how the Designer is applied:
    <Designer(GetType(cTabControlDesigner))> _
    Public Class cTabControl
    The cTabControlDesigner class enables the Design-time tab switching, and also adds the DesignerVerbs (or Smart-Tags) "Add TabPage", "Insert TabPage" and "Remove TabPage".

    Then, there is the custom TabPage cTabPage class. This is merely a usercontrol Inheriting from the windows TabPage, with one extra Property: TabHeaderContextMenuStrip. This is the ContextMenuStrip that shows when you rightclick the tab's header (and can be set via code OR in the designer).
    You can also see that the cTabPage uses the ScrollableControlDesigner which is a standard designer.


    Please note that this is still a work in progress. There are many things that don't work properly yet.
    For example:
    - don't try to drag a TabPage via the 'move icon' in the designer, VS will crash.

    An example project is attached. You may need to Build or run the project to get rid of any warnings.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jul 2009

    Re: [UPDATE] Custom TabControl incl Designer, Tab-specific ContextMenus and Tab-Dragg

    I love it, thanks.

    Firstly, a tip: If you don't want the cursor changing (let it stay the default one), add this to cTabControl.vb:

    Protected Overrides Sub OnGiveFeedback(ByVal e As System.Windows.Forms.GiveFeedbackEventArgs)
    	e.UseDefaultCursors = False
    End Sub
    Secondly, however, the control flickers alot on my computer when dragging, especially when there is a complex control on it (there is an advanced rich text editor on the tab page). Is there any way to solve this?


  3. #3

    Thread Starter
    Join Date
    Apr 2007
    The Netherlands

    Re: [UPDATE] Custom TabControl incl Designer, Tab-specific ContextMenus and Tab-Dragg


    I didn't even remember this control, haha. I found a way around the cursor problem. The problem is that I initiate a drag operation immediately when the cursor moves. That's not the default behavior for dragging, usually the user has to drag the mouse outside of a small rectangle (the size of this rectangle can be obtained from SystemInformation.DragSize or something), but I did not implement that here because I didn't know about it.

    As for flickering, that is most probably because when you are dragging the control is constantly removing and re-adding all tabpages (at least when you drag the page to another position). I don't know any way around this at the moment.

  4. #4
    New Member
    Join Date
    Feb 2013

    Re: [UPDATE] Custom TabControl incl Designer, Tab-specific ContextMenus and Tab-Dragg

    ty for this control. but i have one question, how can i add tab on runtime? i mean i can do that! i add a tab like this:


    and when i select this tab i get this error:

    '// Returns the TabPage whose TabHeader is located at the point 'pt'
    Private Function GetTabPageFromPoint(ByVal pt As Point) As cTabPage

    Dim tp As TabPage = Nothing

    For i As Integer = 0 To Me.TabPages.Count - 1
    If Me.GetTabRect(i).Contains(pt) Then
    tp = Me.TabPages(i)
    Exit For
    End If

    Return CType(tp, cTabPage) '--ERROR---> Unable to cast object of type 'System.Windows.Forms.TabPage' to type 'test.cTabPage'.
    End Function

    can anyone help me?! tNx

    (sry for bad english )

  5. #5
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Deep South, USA

    Re: [UPDATE] Custom TabControl incl Designer, Tab-specific ContextMenus and Tab-Dragg

    How do I get the "X" icon on the tab for closing? I saw a graphic sample of your control and was immediately interested. I am using another design and not pleased with appearance.
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

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