Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Colour on certain Items in a Listbox 2

Status
Not open for further replies.

61cygni

IS-IT--Management
Jul 16, 2002
16
0
0
GB
Hi,

I hope someone out there can help with this.

I have a listbox which has items added to it via a textbox and a button I'll be entering numbers in the textbox press the button and that adds it to the listbox.

This bit works fine. However what I can't work out is how to do the following.

I'd like to press another button and the items in the listbox which equal "1234" will turn Red and everything else will remain Black.

Kind Regards

Steve K


 
You'll need to switch to a listView control.

Use the following code to modify the forecolor.

Code:
Usage:
setItemColor(Me.listview1,"test",color.red)

 Private Sub setItemColor(ByVal lstvw As ListView, ByVal text As String, ByVal clr As Color)
        Dim i As Integer
        For i = 0 To lstvw.Items.Count - 1
            If lstvw.Items(i).Text = text Then
                lstvw.Items(i).ForeColor = clr
            End If
        Next
    End Sub

Scott
Programmer Analyst
<{{><
 
If you want some really cool videos on the subject
goto


And you can download the code from
look for
"View PowerPoint Slides Windows Forms Controls: Tips and Tricks "

Also covers dynamic siziing of entrys in a listbox adding graphics and MUCH MUCH More...

The first link points to MSDN's Webcasts - worth checking out every so often.. Lots of GOOD free training.

Rob
 
The first tip was for a web datagrid. It is easiest to use a ListView.

You can, however, use a ListBox. You need to change the DrawMode to OwnerDraw. Here is a routine that will do what you are asking for. Note that you may want to create your own class to inherit from the ListBox.

Code:
Private Sub DrawItems(ByVal HighlightText As String)
        Dim g As Graphics = ListBox1.CreateGraphics
        Dim BlackBrush As New SolidBrush(Color.Black)
        Dim RedBrush As New SolidBrush(Color.Red)
        Dim WhiteBrush As New SolidBrush(Color.White)
        Dim NavyBrush As New SolidBrush(Color.Navy)
        Dim s As String
        Dim ItemCounter As Integer
        Dim ItemNum As Integer
        ItemCounter = 0
        For Each s In ListBox1.Items
        'If the item is selected, draw a blue rectangle and white text
            If ListBox1.SelectedIndex = ItemNum Then
                g.FillRectangle(NavyBrush, 0, ItemCounter, ListBox1.Width, ListBox1.ItemHeight)
                g.DrawString(s, ListBox1.Font, WhiteBrush, 2, ItemCounter)
            Else
                g.FillRectangle(WhiteBrush, 0, ItemCounter, ListBox1.Width, ListBox1.ItemHeight)
                If Trim(s) = Trim(HighlightText) Then
                    g.DrawString(s, ListBox1.Font, RedBrush, 2, ItemCounter)
                Else
                    g.DrawString(s, ListBox1.Font, BlackBrush, 2, ItemCounter)
                End If
            End If
            ItemCounter += ListBox1.ItemHeight
            ItemNum += 1
        Next

        g.Dispose()
        BlackBrush.Dispose()
        RedBrush.Dispose()
        WhiteBrush.Dispose()
        NavyBrush.Dispose()
End Sub

Now, call the routine in your Form's Paint() event, and your ListBox's SelectedIndexChanged().

Code:
DrawItems("1234")

What this will do is check which string you are looking for. If the string item in your ListBox is equal to what you set, then it will draw it red, else it will draw it black. It will also HighLight your Selected Item.
 
Thanks for all your posts, were a big help

Thanks once again and if my software makes a million I owe you all a Mcdonalds Happy Meal

Kind Regards,

Steve K

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top