Solved

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

Posted on 2004-08-16
4
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

15 Experts available now in Live!

Get 1:1 Help Now