All about me, my life & my work

Tuesday, October 25, 2005

Changing Colour of Listbox Items in VB.NET (.NET 2.0 Beta)

Its simple but finding the code for it on the Internet, and after getting the piece of code, cooking & making it work is bit difficult. So I thought let's put the steps on my blog to help others. This is windows application creating in Microsoft Visual Basic 2005 Express Edition Beta. Here in order to change the colour of items in our Listbox depending on our logic, we have to draw each item in the Listbox. That means, while rendering the Listbox items we have to select the brush, font, colors, etc and draw image inside the Listbox. The steps are:

  1. For the "Listbox" change "DrwaMode" from 'Normal' to 'OwnerDrawFixed'.

  2. Click on "Events" icon on "Properties" window, to create a method for "DrawItem" behaviour. Mine it is 'lstSitename_DrawItem ……… Handles lstSitename.DrawItem'. This method get invoke while rendering each item in the list box.

  3. So while rendering each item, we have to check whether the item is selected or not. If selected then we have to highlight the item by changing the background colour. Otherwise we have to draw it in normal colors. Generally white background & black text for normal item and for selected item, light blue (CornflowerBlue) background & white text.

  4. Now you want to render some items in the list box as per your logic. Suppose if an item is disabled you want to show that one with text strike off & highlight in brown colour.
You ask, 'I already binded/added the Listbox control somewhere else with DataSet or with some other source. How do I know now whether the item is disabled or not now!'
Yes, you don't have any database connection or DataSet to check the rendering item property in order to set text effect & color. But you have to have it. After binding/adding the list items to Listbox, you have to keep that datasource or DataSet in global variable or in persistent place. So that you can access the same in 'DrawItem' method. In this method while setting brush properties, you cross check current item property in datasource is enabled or disabled. According to that you set the text effects & colours. Please see the code below.

Global Variable
 Private SettingsDataSet As DataSet = Nothing

DrawItem Method

Private Sub lstSitename_DrawItem
(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs)
Handles lstSitename.DrawItem
e.DrawBackground()

Dim myBrush As Brush
Dim myBackColor As Color = Color.White
Dim myForeColor As Color = Color.Black
Dim myFont As Font = Me.Font

If (e.State And DrawItemState.Selected)
= DrawItemState.Selected Then
myBackColor = Color.CornflowerBlue
myForeColor = Color.White
End If

'Checking the property of item in DataSet
If isDisabled(lstSitename.Items(e.Index)) Then
myFont = New Font(Me.Font, FontStyle.Strikeout)
myForeColor = Color.Maroon
End If

myBrush = New SolidBrush(myBackColor)
e.Graphics.FillRectangle(myBrush, e.Bounds)
myBrush = New SolidBrush(myForeColor)
e.Graphics.DrawString(lstSitename.Items(e.Index),
myFont, myBrush, New RectangleF(e.Bounds.X,
e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()

End Sub

Property Checking

Private Function isDisabled(ByVal s As String)
As Boolean
Dim rtn As Boolean = False
Dim rowData As DataRow
Dim dvSettings As New DataView(
SettingsDataSet.Tables("site"))
Dim indx As Integer
dvSettings.Sort = "name"
If lstSitename.SelectedItems.Count > 0 Then
indx = dvSettings.Find(s)
rowData = dvSettings(indx).Row
If rowData("disabled").ToString.Trim = "1" Then
rtn = True
End If
End If
dvSettings.Dispose()
dvSettings = Nothing
rowData = Nothing
Return rtn
End Function

If you need any help please post comment or mail me.

12 Comments:

Anonymous Anonymous said...

Ramana
Great blog entry. I have been coding for decades and cannot believe how somthing that should be so simple just isn't. It seems like not everything you need is fully exposed so you have to go to extremes to do something that should be a standard property of the listbox.
Thanks again...

Sun Jul 02, 10:59:00 PM PDT

 
Anonymous Anonymous said...

Thank you. Much appreciated.

Thu Mar 15, 07:43:00 PM PDT

 
Anonymous Anonymous said...

Thank you. Much appreciated.

Thu Mar 15, 07:44:00 PM PDT

 
Blogger Chandhu said...

Wonderful work!....but ever thought about how to highlight particular characters of an item in a ListBox? For ex: We have so many items in a listbox such as Car, Carrot, Carlos..etc and how we can high light/change color only "ar" for each item in the listbox??????

Mon Jul 09, 03:40:00 AM PDT

 
Anonymous Anonymous said...

Yes this is very nice post

Wed Jul 11, 01:23:00 AM PDT

 
Anonymous Anonymous said...

Thanks. Very nice code. I was researching this problem and was getting all kind of complex ideas but this was a very simple solution. Thanks again !

Fri Jul 25, 02:24:00 PM PDT

 
Anonymous Anonymous said...

Great code! I was able to adapt it to my app in a matter of minutes and it just worked. Exactly what I needed. Thanks

Mon Mar 16, 01:31:00 PM PDT

 
Anonymous Anonymous said...

really good code, thank you.

Wed May 27, 05:06:00 PM PDT

 
Anonymous Anonymous said...

the scrollbars isnt showing? am i missing something?

Wed Jun 03, 09:07:00 PM PDT

 
Anonymous Anonymous said...

Post your code.

Wed Jun 03, 11:11:00 PM PDT

 
Anonymous Sean said...

Excellent! I have been messing trying to get something like this working for ages. I have tried using Flex Grids and all sorts before finding your code.

Many thanks.

Tue Jul 28, 01:29:00 AM PDT

 
Anonymous Karim said...

Thanks for sharing,
But i have a littel problem here.

I coding right now mp3 player and it's have a listbox, when the user click play button the background for the selected items is choosen by defualt suppuose the color is "white". My problem is when i select another song i whant the "white" color changed to "gray" and the background for the new seclected items will be "white" color.

I tried to use your's but it's seem not work with me.

Mon Nov 02, 01:00:00 AM PST

 

Post a Comment

Links to this post:

Create a Link

<< Home