VB experts please help! -> 125 pts. for ListBox question

I have a ListBox on my form.

It consists of 10 names.

Is there a way for me to color code the names?  For example, I would like to select the 1st name in the listbox and hit "g" on the keyboard to highlight the 1st name in green color.

Then I'd like to select the 2nd name in the listbox and hit "b" on the keyboard to highlight the 2nd name in blue color.

Then let's say the focus moves to another control.  I would like the ListBox to still retain its green highlight on the 1st name and a blue highlight on the 2nd name.  Is this possible to do?  
Who is Participating?
StimphyConnect With a Mentor Commented:
Try a listView instead..
Private Sub Form_Load()
    Dim cnt As Integer
    ListView1.View = lvwList
    For cnt = 1 To 10
        ListView1.ListItems.Add , , cnt
        ListView1.ListItems(cnt).ForeColor = QBColor(cnt)
End Sub

As far as i know theres no easy way to set the color of individual rows in a list box - you might be better off using the flexgrid - you can then simply set the color of an individual row by using the .CellBackColor property.

You will need to set the color for each column in the grid for the given row ... like this

(assumes you have a flexgrid called flxYourGrid)

public sub setColor(rownum as long, color as string)
dim colnum as long
        colnum = 0
        'loop through all the columns in the grid
        Do While colnum < .Cols
            .row = rownum
            .col = colnum
            If col = "green" Then .CellBackColor = RGB(0, 200, 0)
            If col = "blue" Then .CellBackColor = RGB(0, 0, 200)
            colnum = colnum + 1
    End With
end sub

then just put the stuff in the keydown event of the flex grid to call the above passing in the current rownumber (flxYOURGRID.row) and the color ("green" if the g key is pressed etc)
The standard list box cannot be color coded [unless you do so via bitblt or similar apis]
You don't mention which language or which version you're using; I'm not aware of any way to handle this in VB6 or VBA but in .NET (either C# or VB) you can tie in to the DrawItem event and handle the drawing yourself for each item.   If you're using VB6 or VBA the easiest method is probably the one everyone else mentioned; use a different control type that allows per-item font control and make it look and behave like a listbox.  ListView, as Stimphy suggested, seems to be the best match for what you want to accomplish.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.