Click here to Skip to main content
16,005,290 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB
Public Sub Add_In_ListView(ByVal Field1 As String, ByVal Field2 As String, ByVal Field3 As String, ByVal Field4 As String, ByVal Field5 As String, ByVal Field6 As String, ByVal Field7 As Boolean)
    lv = ListView1.Items.Add(Field1)
    lv.SubItems.Add(Field2)
    lv.SubItems.Add(Field3)
    lv.SubItems.Add(Field4)
    lv.SubItems.Add(Field5)
    lv.SubItems.Add(Field6)
    If Field7 = True Then
        lv.SubItems.Add(6).Text = "Enable"
        lv.SubItems(6).ForeColor = Color.Green
    Else
        lv.SubItems.Add(6).Text = "Disable"
        lv.SubItems(6).ForeColor = Color.Red
    End If
    lv.SubItems.Add("")
    lv.SubItems.Add("")
    lv.SubItems.Add("")
    lv.SubItems.Add("")
    lv.SubItems.Add("")
    ListView1.Refresh()
End Sub



When my subroutine run add correctly the line with the word enable or disable in the listviw BUT they did not change the color.
Why???

Thanks and regards

[EDIT]
Mea culpa

I have forget the
lv.UseItemStyleForSubItems = False

Sorry!
Posted
Updated 15-Mar-11 8:50am
v3
Comments
Sergey Alexandrovich Kryukov 15-Mar-11 14:49pm    
I answered at the same, time; you posted your update when I was testing.
So, please formally accept my Answer :-)
--SA

1 solution

The resolution found: for the ListViewItem variable lv, set the property UseItemStyleForSubItems to false.
According to MSDN on the topic of properties ForeColor and BackColor:

If the UseItemStyleForSubItems property of the ListViewItem that owns the subitem is set to true, setting this property has no effect.
Tested.

Good luck.

—SA
 
Share this answer
 
v3
Comments
fjdiewornncalwe 15-Mar-11 14:51pm    
+5. This should be marked as correct answer.
Sergey Alexandrovich Kryukov 15-Mar-11 15:00pm    
Thank you very much. Even though OP found right answer already...
--SA
Sergey Alexandrovich Kryukov 15-Mar-11 22:04pm    
Vagelisr,
Thank you for accepting my Answer, good luck.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900