Solved

# Listbox color ??? 500 easy points!

Posted on 2005-05-02
301 Views
I have a listbox that has 3 or 4 different colors, How can I have a message box or a string display what the color of the selected item in the listbox is...This question is urgent and worth 500 points!
0
Question by:juravich

LVL 96

Expert Comment

I am assuming that you have an owner-drawn listbox displaying the colors.

Is this a valid assumption?  If so, then how are you accomplishing this?

Bob
0

LVL 2

Author Comment

This is how im displaying the colors. I'm afraid this is the only way that i know how to do it.

Private Sub CumulativeBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles CumulativeBox.DrawItem
Dim materialInside As String
Dim materialboolean As Boolean

e.DrawBackground()

materialInside = CumulativeBox.Items(e.Index)

Dim Br As Brush
If (CumulativeBox.Items(e.Index)) = vbTab & "Action Changes" Or (CumulativeBox.Items(e.Index)) = vbTab & "Code Changes" Then
Br = Brushes.DarkGray
ElseIf materialInside.StartsWith(vbTab & vbTab) Then
Br = Brushes.Red
Else
Br = Brushes.RoyalBlue
End If

e.Graphics.DrawString(CumulativeBox.Items(e.Index), e.Font, Br, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()

End Sub
0

LVL 96

Accepted Solution

(1) Create an array of colors:

Private itemColors() As Color

(2) After filling the list box:

Redim itemColors(CumulativeBox.Items.Count - 1)

(3) Add a tooltip component to the form.

(4) When drawing each item:

If (CumulativeBox.Items(e.Index)) = vbTab & "Action Changes" Or (CumulativeBox.Items(e.Index)) = vbTab & "Code Changes" Then
Br = Brushes.DarkGray
itemColors(e.Index) = Color.DarkGray
ElseIf materialInside.StartsWith(vbTab & vbTab) Then
Br = Brushes.Red
itemColors(e.Index) = Color.Red
Else
Br = Brushes.RoyalBlue
itemColors(e.Index) = Color.RoyalBlue
End If

Me.Tooltip1.SetTooltip(CumulativeBox, itemColors(e.Index).Name)

Bob
0

LVL 2

Author Comment

Let me explain a little further what i have...

I have the listbox (cumulativebox), in this listbox there are a bunch of different items, all of different type. Each type has been given a color by using the drawing item above. I just need a way to find out what the type of item is (by using the color).

i.e. Severe = red

I want to find out what is severe by seeing if the color is red.

Thanks alot!
0

LVL 96

Expert Comment

Do you want this information as a user interface item, or a debug item?

All I was trying to do is come up with a solution to keep track of the colors for each item in an type-specific Color Array, so that you can query it.

Bob
0

LVL 2

Author Comment

ohh ok, well im gonna give your previous solution a try and see what i can find out.

I am using this information as a user interface item.
0

LVL 2

Author Comment

I thought about your suggestion and used the logic to create something very similar to your code that fits my needs perfectly. Thank you very much for the insight!!!
0

## Featured Post

### Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…