Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

ListBox BackColor....500 Points

I am looking for a way to make everyother row have a different background color....

I tried using modulus with the drawitem method...however it is not working for me...

Any Suggestions???
0
juravich
Asked:
juravich
  • 3
  • 2
1 Solution
 
heintalusCommented:
Just threw this together which seems to work well

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim BrushBG As Brush
        Dim BrushText As Brush

        e.DrawBackground()

        If (e.Index Mod 2) = 0 Then
            BrushBG = Brushes.LimeGreen
            BrushText = Brushes.Red
        Else
            BrushBG = Brushes.Black
            BrushText = Brushes.Yellow
        End If

        ' Draw background in selected color
        e.Graphics.FillRectangle(BrushBG, e.Bounds)
        e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds)

        ' Draw the current item text based on the current Font and the custom brush settings.
        If e.Index <> -1 Then
            e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, e.Font, BrushText, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        End If

        ' If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle()
    End Sub

Dont forget to set the listbox DrawMode to OwnerDrawVariable

HTH
Andy
0
 
gregasmCommented:
Are you GDI+ MAN?
0
 
heintalusCommented:
????????????
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gregasmCommented:
sorry, sometimes I write random things that make no sense.

I was asking a rhetorical question... one that both has no meaning and makes no sense at the same time...

What I ought to have said was "hey, that's pretty good for something that you threw together in no time at all." =]
0
 
heintalusCommented:
LOL, when I saw the question I thought to myself that would be a useful thing to be able to do so gave it a try & hey presto it actually worked 8)
0
 
juravichAuthor Commented:
Thanks! that works perfectly!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now