ListBox BackColor....500 Points

Posted on 2005-05-16
Last Modified: 2012-06-21
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???
Question by:juravich
    LVL 10

    Accepted Solution

    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


            If (e.Index Mod 2) = 0 Then
                BrushBG = Brushes.LimeGreen
                BrushText = Brushes.Red
                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.
        End Sub

    Dont forget to set the listbox DrawMode to OwnerDrawVariable

    LVL 8

    Expert Comment

    Are you GDI+ MAN?
    LVL 10

    Expert Comment

    LVL 8

    Expert Comment

    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." =]
    LVL 10

    Expert Comment

    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)
    LVL 2

    Author Comment

    Thanks! that works perfectly!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now