Subject: listbox, different color for each item?
Posted By: MichaelTJ Post Date: 11/3/2003 7:08:23 PM
Hi all,
I was just wondering if it's possible to change the text color or background color for an item in a listbox? Eks. you have an SQL querry string that gets some values. If one of the values is 1 then the color is normal. If the value is 2 then the color turns red! Is this possible?

***************MY CODE FOR FILLING THE LISTBOX********
    Sub FillList(ByVal Level As Int32, ByVal SQL1 As String)
        Dim SQ As Data.SqlClient.SqlDataReader
        Dim SQLConnect As Data.SqlClient.SqlConnection
        SQLConnect = SQL.Connect
        Dim SQLCmd As New System.Data.SqlClient.SqlCommand("set dateformat dmy", SQLConnect)
        ListColl(Level).Items.Clear()
        Try
            SQLCmd.CommandText = (SQL1)
            SQ = SQLCmd.ExecuteReader()
            Dim i As Integer
            Do While SQ.Read = True
                If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
            Loop
            SQ.Close()
        Catch ex As Exception
            Response.Write("Fill list: " & ex.Message)
        End Try
        SQLCmd.Connection.Close()
        SQLConnect.Close()
    End Sub
************END CODE*********************

I need something like this:
************CODE FROM ABOVE*********
Do While SQ.Read = True
If SQ(2) = 1 then                
If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
else
*Some Color setting for that item*
If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
end if
Loop
***************END******************

Thanks for any help I can get on this!

------------------------
All help is Good help!
Regards
Michael
Reply By: KenSchaefer Reply Date: 11/13/2003 9:37:30 PM
Create a new ListItem. Set the cssClass to the appropriate CSS class that you want to use. Then add this ListItem to your DropDownList's Items collection. So you should end up with:

<option class="itemA">Some Value</option>
<option class="itemB">Some Other Value</option>

and then in your CSS file (or inline) you have:

itemA
{
   color: #FF0000;
}
itemB
{
   color: #00FF00;
}

Cheers
Ken



Microsoft MVP - Windows Server (IIS)
www.adOpenStatic.com

Go to topic 6435

Return to index page 1006
Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997