Results 1 to 13 of 13

Thread: Changing TabControl Backcolor

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    26

    Changing TabControl Backcolor

    Hi,

    I tried to change the TabControl back color but only succeeded changing the tabs back color (with the code below) but not the tab control color (which is behind the tabs, in the upper right corner).

    HELP!

    many thanks






    Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem

    'Firstly we'll define some parameters.
    Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
    Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
    Dim FillBrush As New SolidBrush(Color.LightGray)
    Dim TextBrush As New SolidBrush(Color.Black)
    Dim sf As New StringFormat
    sf.Alignment = StringAlignment.Center
    sf.LineAlignment = StringAlignment.Center

    'If we are currently painting the Selected TabItem we'll
    'change the brush colors and inflate the rectangle.
    If CBool(e.State And DrawItemState.Selected) Then
    '' FillBrush.Color = Color.LightGray
    '' TextBrush.Color = Color.Black
    ItemRect.Inflate(2, 2)
    End If

    'Set up rotation for left and right aligned tabs
    If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
    Dim RotateAngle As Single = 90
    If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
    Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
    e.Graphics.TranslateTransform(cp.X, cp.Y)
    e.Graphics.RotateTransform(RotateAngle)
    ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
    End If

    'Next we'll paint the TabItem with our Fill Brush
    e.Graphics.FillRectangle(FillBrush, ItemRect)

    'Now draw the text.
    e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)

    'Reset any Graphics rotation
    e.Graphics.ResetTransform()

    'Finally, we should Dispose of our brushes.
    FillBrush.Dispose()
    TextBrush.Dispose()

    End Sub

  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: Changing TabControl Backcolor

    Are you talking about the unoccupied space to the right of the actual tabs in the tabcontrol? This will take on whatever color the container is. So for example if your forms background color was red, and you place a tabcontrol on it, the tabcontrols area in the upper right will be red as well.

    If it is a case where you want this color to actually be different from the form, then add a panel to your form, and set the panel to whatever color you want. Then move your tabcontrol into the panel, and set its dock property to fill so it takes up the full space of the panel control.

    The TabControl doesn't expose a BackColor property (it inherits it from the base class Control, but it hides it, and has no use)

    In fact, this is totally valid code

    Code:
    TabControl1.BackColor = Color.red
    it will compile and everything, but internally this property is simply ignored by the tabcontrol.

  3. #3

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    26

    Re: Changing TabControl Backcolor

    Yes, i'm talking about unoccupied space to the right of the actual tabs in the tabcontrol. Unfortunattly, none of the advices you gave were helpful

    Any ideas?

    Thank in advance

  5. #5

  6. #6
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Changing TabControl Backcolor

    Not sure I totally know whats going on here but can you try putting the tabcontrol on a panel and then setting its backcolor to the backcolor you want?
    If I helped you please rate me.

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

    Re: Changing TabControl Backcolor

    Quote Originally Posted by ngreenwood6 View Post
    Not sure I totally know whats going on here but can you try putting the tabcontrol on a panel and then setting its backcolor to the backcolor you want?
    See the first reply above

  8. #8
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Changing TabControl Backcolor

    I read the first part of your reply but skipped over the last half lol. Do what he says lol :;
    If I helped you please rate me.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    26

    Re: Changing TabControl Backcolor

    I understood what he was saying it simply doesn't work. I opened a new project with a tab control only trying to isolate the problem.

    Now the space is filled with the form back color, however i cannot change the tabs headers back color. when i do that with the TabControl1_DrawItem function, the headers color is changed to what i want, but the space is changed to some kind of gray and i don't have control over it - back to square one. ((

    i guess my problem in one sentence is : how do i control the entire TabControl back color - tabs, tabs headers and that space in the corner?

  10. #10
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Changing TabControl Backcolor

    ok.. go REread what he instructed to do.

    do NOT drop the TabControl on the form itself. Put a Panel control on the form. THEN, add the TabControl to the panel. Set the PANEL's BackColor property to whatever color you are trying to achieve as the BackColor of the tabcontrol.

    Name:  tabcontrol.JPG
Views: 4527
Size:  50.3 KB

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    26

    Re: Changing TabControl Backcolor

    I already did that and as you can see in the picture, the tabs headers backcolor is not changed, only the space in the upper right corner. I need to change both.

    I managed to overcome the problem by adding the following code:

    Dim r As Rectangle = TabControl1.GetTabRect(TabControl1.TabPages.Count - 1)
    Dim rf As RectangleF = New RectangleF(r.X + r.Width, r.Y - 5, TabControl1.Width - (r.X + r.Width), r.Height + 5)
    Dim b As Brush = New SolidBrush(Color.Yellow)
    e.Graphics.FillRectangle(b, rf)
    b.Dispose()

    I got the code from the following links:

    http://blog.villainousmind.com/2009/...ol-in-net.html
    which led me to
    http://stackoverflow.com/questions/6...ontrols-in-net
    Last edited by kiss987; Jul 9th, 2009 at 02:24 AM.

  12. #12

  13. #13

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