Solved

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

Posted on 2004-08-16
4
211 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

813 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

10 Experts available now in Live!

Get 1:1 Help Now