Change Backcolor Of Listbox Item and keep this Color When Another Item is Added

shauno100

Member
Joined
Oct 10, 2016
Messages
8
Programming Experience
1-3
Hi,

I have written a VB.NET program which successfully sets the backcolor to either LightGreen or LightCoral for a Listbox item which is reliant on a condition statement.

The issue i am having is say that the first item to be added to the listbox meets the LightGreen criteria it gets added with the correct backcolor but when the next item to be added meets the Lightcoral criteria it overwrites the color of the first listbox item also instead of just its backcolor.

Is there away to set each items backcolor so that it keeps its backcolor and as each additional item is added this does not also alter the color of the other items?

I am using the below code

VB.NET:
 Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles SRESGroupList.DrawItem
        e.DrawBackground()


        If (e.State And DrawItemState.None) = DrawItemState.None Then
            e.Graphics.FillRectangle(HiLiteBrush, e.Bounds)
        End If
        Using b As New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(SRESGroupList.GetItemText(SRESGroupList.Items(e.Index)), e.Font, b, e.Bounds)
        End Using
        e.DrawFocusRectangle()
    End Sub

The condition statement is basically like this

VB.NET:
 If SendAsPermission.Contains("S-RES") = True Then
                            HiLiteBrush.Color = Color.LightGreen
                           
                        Else
                            HiLiteBrush.Color = Color.LightCoral
                          


                        End If

SendAsPermission is a string type.

Thanks in advance.
 
Last edited:
You've only got one brush that you use to draw every item every time so that means that every time an item gets redrawn it will be drawn using the current colour, regardless of what the current colour was when it was first added. You need to remember what that current colour was so that you can use it again.

One option would be to keep a List(Of Color). Each time you add an item, determine what colour it should be and add that Color to your List. In the DrawItem event handler, you use the item index to get the corresponding Color from the List and use that to draw the item.
 
You've only got one brush that you use to draw every item every time so that means that every time an item gets redrawn it will be drawn using the current colour, regardless of what the current colour was when it was first added. You need to remember what that current colour was so that you can use it again.

One option would be to keep a List(Of Color). Each time you add an item, determine what colour it should be and add that Color to your List. In the DrawItem event handler, you use the item index to get the corresponding Color from the List and use that to draw the item.


Thanks for the reply, i am a bit of a novice when it comes to VB.net in some areas, would you be able to provide an example possibly how i could select the colours and use the current listbox index in the Drawitem handler. I'm assuming i need to have an increment to increase the index count per listbox item.

Basically the calling code is occurring within an

VB.NET:
For i = 0 To counter2 - 1
 If SendAsPermission.Contains("S-RES") = True Then


                            HiLiteBrush.Color = Color.LightGreen
                             
                        Else
                            HiLiteBrush.Color = Color.LightCoral
               

                        End If
 Next

Thanks again for your assistance.
 
Last edited:
No, you don't need an increment. You need to do what I said.
One option would be to keep a List(Of Color). Each time you add an item, determine what colour it should be and add that Color to your List.
Which part of that is confusing you?
 
No, you don't need an increment. You need to do what I said.Which part of that is confusing you?

The part that is confusing me is the vb.net code to make that happen.

In the program the user is able to either select multiple items in another listbox have them processed and the results added to the listbox i want the backcolour on. They are also able to select items one by one to add the results to the listbox.

I am just thinking of the complexity to maintain the correct colors for each item every time the listbox is redrawn which will be occurring everytime an item is added to the list.

Anyway as mentioned i am a vb.net novice hence why i am asking for assistance.
 
There is no complexity. You create a List. You add items to that List as you add items to the ListBox. In the DrawItem event handler, you get an item from that List by index. I've told you what you need to do. I don't write people's code for them.
 
private listboxcolors as new list(of color)
'...
listbox.items.add("something")
listboxcolors.add(colors.blue)
 
There is no complexity. You create a List. You add items to that List as you add items to the ListBox. In the DrawItem event handler, you get an item from that List by index. I've told you what you need to do. I don't write people's code for them.

Mate I didn't ask you to write the code for me from start to finish. I was after pointers/snippets of code perhaps to give me an idea to expand upon. seeing this is a vb.net forum.

I will take on your philosophy and will work something out.
 
I was after pointers/snippets of code perhaps to give me an idea to expand upon.
I gave you pointers. I told you things that you didn't know before. Did you try to make use of them? I didn't see any evidence of that. Did you use that information to look for relevant code examples? I didn't see any evidence of that. When the required code is as simple as it is in this case, writing a snippet is writing it from start to finish. There's really only one line required that JohnH hasn't provided. I told you to create a List. That's one line of code. How many pointers and snippets do you need to do that? I told you to add a Color to that List. That's one line of code. I told you to get a Color from that List by index. That's one line of code. It's not like there's an algorithm to implement. If you're not prepared to make an effort, neither am I. If you try and fail then I'll help you fix it but if you don't try then neither will I.
 
Fair enough mate, i got it working in the end anyway by adding a comma to the end of the result if a condition was met. Any item in listbox containing the comma would be coloured green and everything else a lightcoral colour. Not the tidiest way i guess but i am happy it working. It looks like the drawitem mode is getting rid of the comma anyway after its drawn as i cannot see the comma. Below was the code i used
VB.NET:
       Private Sub SRESGroupList_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles SRESGroupList.DrawItem
        e.DrawBackground()


        If SRESGroupList.Items(e.Index).ToString().Contains(",") Then
            e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
            Using b As New SolidBrush(e.ForeColor)
                e.Graphics.DrawString(SRESGroupList.GetItemText(SRESGroupList.Items(e.Index)), e.Font, b, e.Bounds)
            End Using
            e.DrawFocusRectangle()
        Else
            e.Graphics.FillRectangle(Brushes.LightCoral, e.Bounds)
                Using b As New SolidBrush(e.ForeColor)
                    e.Graphics.DrawString(SRESGroupList.GetItemText(SRESGroupList.Items(e.Index)), e.Font, b, e.Bounds)
                End Using
                e.DrawFocusRectangle()
            End If










    End Sub


Thanks for the assistance.
 
Back
Top