Solved

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

Posted on 2004-08-16
4
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

738 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