Results 1 to 9 of 9

Thread: [RESOLVED] Tab Control questions.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Resolved [RESOLVED] Tab Control questions.

    I am wanting to do a couple things with a tab control.

    1) Change the back/fore color of the actual tab in the tabcontrol, not the tabpage background.
    2) Handle the MouseDown event of the actual tab, not the tabpage background.

    When I do tabPage.ForeColor = Color.Red, it changes the background of the tabpage, and same thing with the MouseDown event. Is there a (somewhat) simple method of doing the above 2 things for the actual tab?

    Thanks in advance.
    Last edited by Tool; Dec 21st, 2006 at 02:42 PM. Reason: Resolved (Thanks keinma and stanav)

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Tab Control questions.

    1) You need to set the drawmode to owner draw, and draw the tabs yourself.
    2) I am not sure what you mean by handle the MouseDown of the actual tab. Both the individual tabs, and the tabcontrol itself, have MouseDown events that you can write code in.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Tab Control questions.

    Use "owner draw" to do this.... This is a slightly modified version of the example found on MSDN. (You need to set your TabControl's DrawMode property to OwnerDrawFixed, and do the drawing in DrawItem event of the TabControl.)
    VB Code:
    1. Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
    2.         Dim g As Graphics = e.Graphics
    3.         Dim _TextBrush As Brush
    4.         Dim pageNum As Integer = e.Index
    5.        
    6.         ' Get the item from the collection.
    7.         Dim _TabPage As TabPage = TabControl1.TabPages(pageNum)
    8.  
    9.         ' Get the real bounds for the tab rectangle.
    10.         Dim _TabBounds As Rectangle = TabControl1.GetTabRect(pageNum)
    11.  
    12.         If (e.State = DrawItemState.Selected) Then
    13.             ' Draw a different background color, and don't paint a focus rectangle.
    14.             _TextBrush = New SolidBrush(Color.Red)
    15.             g.FillRectangle(Brushes.Yellow, e.Bounds)
    16.         Else
    17.             'The TabControl has 2 TabPages, thus select case only goes to 1.
    18.             'Add more Case if you have more tabpages.
    19.             Select Case pageNum
    20.                 Case 0
    21.                     g.FillRectangle(Brushes.Brown, e.Bounds)
    22.                 Case 1
    23.                     g.FillRectangle(Brushes.Blue, e.Bounds)
    24.                 Case Else
    25.                     g.FillRectangle(Brushes.Green, e.Bounds)
    26.             End Select
    27.             _TextBrush = New System.Drawing.SolidBrush(e.ForeColor)
    28.         End If
    29.  
    30.         ' Use our own font.
    31.         Dim _TabFont As New Font("Times New Roman", 10.0, FontStyle.Bold, GraphicsUnit.Pixel)
    32.  
    33.         ' Draw string. Center the text.
    34.         Dim _StringFlags As New StringFormat()
    35.         _StringFlags.Alignment = StringAlignment.Center
    36.         _StringFlags.LineAlignment = StringAlignment.Center
    37.         g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags))
    38.     End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Re: Tab Control questions.

    Thank you keinma and stanav, I guess ownerdraw is the only way to do it (was hoping it would be a bit easier). That answered my first question.

    kleinma:
    As for the second question, I want to have a context menu on the tab itself on right click, and close on middle click. Best example I can think of is almost any tabbed web browser.

    I created my own class that inherits from TabPage. When I handle MouseDown on that, it only gets called when the TabPage gets the Mouse Down. I am guessing I will have to write my own code to figure out which tab is selected (similar to owner draw and changing the background color of the selected tab). Is this the case, or is there a simpler way of handling that?

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Tab Control questions.

    Like I mentioned earlier, use the mousedown of the tab control itself, not the tabpage.

    VB Code:
    1. Private Sub TabControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseDown
    2.         If e.Button = Windows.Forms.MouseButtons.Right Then
    3.             ContextMenuStrip1.Show(TabControl1, e.X, e.Y)
    4.         End If
    5.     End Sub

    the tabcontrol itself has its own full set of events, just like each tab page has a set of events. the mousedown of the tabcontrol only fires when the mouse is clicked on one of the tabs.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Re: Tab Control questions.

    Sorry, misunderstood your first post. That is, indeed, what I was looking for. Thank you again for your help, much appreciated.

  7. #7
    New Member
    Join Date
    May 2006
    Posts
    7

    Re: [RESOLVED] Tab Control questions.

    VB Code:
    1. g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags))

    (141): Overload resolution failed because no accessible 'DrawString' can be called with these arguments:
    'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, layoutRectangle As System.Drawing.RectangleF, format As System.Drawing.StringFormat)': Value of type 'System.Drawing.Rectangle' cannot be converted to 'System.Drawing.RectangleF'.
    'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, point As System.Drawing.PointF, format As System.Drawing.StringFormat)': Value of type 'System.Drawing.Rectangle' cannot be converted to 'System.Drawing.PointF'.
    'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, x As Single, y As Single)': Value of type 'System.Drawing.Rectangle' cannot be converted to 'Single'.
    'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, x As Single, y As Single)': Value of type 'System.Drawing.StringFormat' cannot be converted to 'Single'.


    using vb2003 why am I getting this error? Used the code from above post.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] Tab Control questions.

    just a guess, but maybe you are trying to pass in a type of "rectangle" when it wants a type of "rectangleF"

    the difference between the 2 is that rectangleF uses singles and rectangle uses integer. That means a rectangleF can have a side that is 2.5 and not just 2 or 3

    if _TabBounds is indeed a rectangle, you can use

    VB Code:
    1. RectangleF.op_Implicit(_TabBounds)

    to convert a rectangle to rectangleF

  9. #9
    New Member
    Join Date
    May 2006
    Posts
    7

    Re: [RESOLVED] Tab Control questions.

    Thanks Kleinma

    got it with

    VB Code:
    1. Dim _TabBounds As RectangleF = RectangleF.op_Implicit(TabControl1.GetTabRect(pageNum))

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