Results 1 to 10 of 10

Thread: [RESOLVED] Tabpage Colour

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Resolved [RESOLVED] Tabpage Colour

    Hello. Can someone help me please.

    I have a tabcontrol and all of the tabs are coloured red with white writing except for the one the user is using which is then yellow with red writing.

    How do i change the colour of a tab if an item is present in a listview to the colour green with white writing, but if the user is using that tab it changes to yellow with red writing until the user selects a different tab?

    Cheers

  2. #2
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Tabpage Colour

    Basically, your question is "How do you colour the tabs of a Tab Control yes? This thread should help you.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Tabpage Colour

    No, like I said, the tabs are already coloured, I just want when theres at least 1 item in a listview then that tab is then coloured green with with white writing

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Tabpage Colour

    So you know how to colour the tabs? Then what part of ...

    If ListView1.Items.Count > 0

    ... is problematical?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Tabpage Colour

    Ohhh Well you know what im like with coding, not very good at it. I didnt think about Count.

    Shall i send you the whole coding i have for it, so i can confirm where the count goes?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Tabpage Colour

    This is the code im using at the minute to colour the tabs as red with white writing when not being used. And when being used they turn Yellow with Red writing.

    Ive tried placing that code everywhere in here andit works but it changes all of the tabs green when i click on them, i only want it for the tab that the listview is on. I.e the tabe pag is called Assigned

    Code:
    Private Sub TabControl1_DrawItem(ByVal sender As System.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.Red)
            Dim TextBrush As New SolidBrush(Color.White)
            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.Yellow
                TextBrush.Color = Color.Red
                ItemRect.Inflate(2, 2)
            End If
    
            'Set up rotation for left and right aligned tab
            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

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Tabpage Colour

    code Code:
    1. sf.Alignment = StringAlignment.Center
    2.         sf.LineAlignment = StringAlignment.Center
    3.  
    4. >>>>>>>>>>>>>> Add the following code snippet here <<<<<<<<<<<<
    5.        
    6.         'If we are currently painting the Selected TabItem we'll
    7.         'change the brush colors and inflate the rectangle.
    8.         If CBool(e.State And DrawItemState.Selected) Then

    vb.net Code:
    1. If e.Index = TabControl1.TabPages.IndexOf(TabControl1.TabPages("Assigned")) And ListView1.Items.Count > 0 Then
    2.             FillBrush.Color = Color.Green
    3.             TextBrush.Color = Color.White
    4.         End If

    NB. This requires that the TabPage is actually named Assigned not just that the Text is "Assigned"
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Tabpage Colour

    Thats great it works now thanks!!!! Although one more thing, it only goes green once i click on that tab and then come away from it again, is there a way to make it so it goes green even if im on a seperate tab?

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Tabpage Colour

    That's probably because the Tabcontrol is drawn before the ListView Items are loaded. Put in a TabControl1.Refresh() after the ListView items load.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Tabpage Colour

    Thats great thanks!!

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