Solved

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

Posted on 2004-08-16
4
210 Views
Last Modified: 2010-04-17
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?  
0
Comment
Question by:CoderBboy
4 Comments
 

Expert Comment

by:mjdmjd
ID: 11816229
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
With flxYOURGRID
        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
        Loop
    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)
0
 
LVL 8

Expert Comment

by:tonsofpcs
ID: 11817736
The standard list box cannot be color coded [unless you do so via bitblt or similar apis]
0
 
LVL 3

Accepted Solution

by:
Stimphy earned 125 total points
ID: 11817841
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)
    Next
End Sub

Regaurds,
Dave
0
 
LVL 5

Expert Comment

by:jmacmicking
ID: 11819360
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 57
When i run adoquery my application freezes 26 152
C# code editing and collaboration 3 99
Base1 Encode/Decode 3 68
This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

867 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

18 Experts available now in Live!

Get 1:1 Help Now