How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

How to draw Listbox items with alternative background colors

Some people find it easier to read large amount of data when the rows have an alternative background color instead of always the same. This is called Zebra Striping.

For this example we will look into implementing this technique for our ListBox control so it will look similar to this:
Listbox zebra striping

First we need to ensure that the ListBox.DrawMode property is set to DrawMode.OwnerDrawFixed. This can be accomplished by either changing the property in the properties window or by placing listBox1.DrawMode = DrawMode.OwnerDrawFixed; in your form’s constructor or any of the load events.

Then we need to handle the ListBox’s DrawItem event so we can change the colors as we like.

And we are done ! Feel free to modify the example to suit your needs.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

5 Responses to How to draw Listbox items with alternative background colors

  1. sandeep says:

    Nice Article to solve my problem

  2. Mukesh Kumar says:

    One thing I would like to add – My List box stopped showing the elements when I set ListBox.DrawMode property to DrawMode.OwnerDrawFixed. When I reset to DrawMode.OwnerDrawVariable, it started working correctly.Didn’t understand why it happened.

  3. Mukesh Kumar says:

    A very helpful article. This is what I was looking for and it offered me a great help and proved to be my savior. Thank you so much.

  4. I spent a lot of time with a broken DisplayMember. says:

    Instead of

    I suggest

    This way, DisplayMember doesn’t get broken.

Leave a Reply

Your email address will not be published. Required fields are marked *