Select Case (e.Index)
            Case 0
                If Alarmlvl = 3 Then
                    bwcolor = Brushes.Gold
                ElseIf Alarmlvl = 2 Then
                    bwcolor = Brushes.Black

                ElseIf Alarmlvl = 1 Then
                    bwcolor = Brushes.BlueViolet
                End If

            Case 1
                bwcolor = Brushes.Gold
                If Alarmlvl = 3 Then
                    bwcolor = Brushes.Gold
                ElseIf Alarmlvl = 2 Then
                    bwcolor = Brushes.Black
                ElseIf Alarmlvl = 1 Then
                    bwcolor = Brushes.BlueViolet
                End If
            Case 2
                bwcolor = Brushes.Gold
                If Alarmlvl = 3 Then
                    bwcolor = Brushes.Gold
                ElseIf Alarmlvl = 2 Then
                    bwcolor = Brushes.Black
                ElseIf Alarmlvl = 1 Then
                    bwcolor = Brushes.BlueViolet
                End If
        End Select
e.Graphics.FillRectangle(bwcolor, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

What I tring to do is when listbox.item.add the forecolor will change according its Alarmlvl. Try to search over net, but no luck. I know if use

Select Case (e.Index) 
           Case 0
                 bwcolor = Brushes.Gold
          Case 1
               bwcolor = Brushes.Black
          Case 2
               bwcolor = Brushes.BlueViolet
          End Select
e.Graphics.FillRectangle(bwcolor, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

can work..but i want the colour change according Alarmlvl and not e.index

Any idea how to work around this? Any advise is appreciated^^

Recommended Answers

All 6 Replies

I am able to use array to make it work. But if use "Select case" then mean I need to type all the possible e.index by hand. Anyway to automatic populate it? cause i think i need array of 200.

Im not quite sure hows the e.index and fillretangle e.bound related, so i dont know any suitable statement beside "Select Case"

Hi,

I don't know of an easy way to do it using a ListBox control.
I would use a ListView control instead. This typically gives
you more features anyway, not to mention. From what I have discovered using the ListBox control, you can only set the ForeColor for the entire control, not the individual items within the control.
If you would like to use the ListView control instead, here some sample code to do what you wanted to do.

Just drag a ListView control onto your form, or build it dynamically,
whichever you prefer, and then add the following code:

Private Sub BuildListView
Dim i As Integer
ListView1.View = View.Details
ListView1.Columns.Add("Column1", 200, HorizontalAlignment.Left)
For i = 0 To 3
Dim x As New ListViewItem
Select Case i
Case 0
x.Text = "Red text goes here..."
x.ForeColor = System.Drawing.Color.Red
Case 1
x.Text = "Blue text goes here..."
x.ForeColor = System.Drawing.Color.Blue
Case 2
x.Text = "Green text goes here..."
x.ForeColor = System.Drawing.Color.Green
Case 3
x.Text = "Black text goes here..."
x.ForeColor = System.Drawing.Color.Black
End Select
ListView1.Items.Add(x)
Next i
End Sub

I hope it will help you in right direction.

For index As Integer = 1 To i-1
                AlarmOF = Mid(Finalalarmbit, i - index, 1)
                If AlarmOF = "1" Then
                    x.Text = index & ")." & "[" & TimeOfDay & "]  " & m_dtContacts.Rows(index)(ColumnName).ToString
                    Alarmlvl = m_dtContacts.Rows(index)("AlmLevel").ToString
                    If Alarmlvl = 3 Then
                        x.BackColor = System.Drawing.Color.Red
                    ElseIf Alarmlvl = 2 Then
                        x.BackColor = System.Drawing.Color.Gold
                    ElseIf Alarmlvl = 1 Then
                        x.BackColor = System.Drawing.Color.Yellow
                    End If
                    AlarmLstBox.Items.Add(x)
                End If

When index =1, the first time is ok, when the next items added(index = 2), Error Occur.
"Cannot add or insert the item 'XXXX' in more than one place. You must first remove it from its current location or clone it. Parameter name: item

I need to Dim another "New ListViewItem"(like dim y as New ListViewItem) for the index(2) and for other?

Sorry,Please ignore above post^^ Seem i Put the Dim X as new listviewitem outside the loop. My bad. It work ok now^^

Ermmm...
Another question, How to add Item to difference column, like i have 3 item,and 3 column, May i have a sample code?

Figure it out aready^o^ Sweats.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.