Change ListView SubItem Forecolor at runtime

When I initially populate a ListView control with ListItems and ListSubItems, I set the forecolor to vbRed.
Once the user finishes any edits to the ListItems, they click a button whose purpose is to then change the ListItem and ListSubItem Forecolor to vbBlack.

My code does change the ListItem (Which is in column 1) color when executed.  However, the other columns, which are ListSubItems, do not change.
Private Sub Post_Set_Click()
    Dim lvItemScan As Integer
    For lvItemScan = 1 To ListView1.ListItems.Count
        ' Set Member Forecolor to black
        ListView1.ListItems(lvItemScan).ForeColor = vbBlack
        ListView1.ListItems(lvItemScan).ListSubItems.Item(1).ForeColor = vbBlack
        ListView1.ListItems(lvItemScan).ListSubItems.Item(2).ForeColor = vbBlack
        ListView1.ListItems(lvItemScan).ListSubItems.Item(3).ForeColor = vbBlack
        ListView1.ListItems(lvItemScan).ListSubItems.Item(4).ForeColor = vbBlack
    Next lvItemScan
    Me.Post_Set.Visible = False
End Sub

Open in new window

Who is Participating?
PaulHewsConnect With a Mentor Commented:
>t was word for word what I had in the example.

No, it's not.  ListSubItems.Count will determine whether there are any subitems to apply the forecolor to.  If Count = 0 then the loop will not iterate, and nothing will error out.  If Count is 1 or greater, then the forecolor will be applied.  Please try it.
BobZZAuthor Commented:
I just noticed the code errors if it encounters a ListItem with no listsubitems.

The following code to test for a ListSubItem and it failed with a "Type Mis-Match)

If (ListView1.ListItems(lvItemScan).ListSubItems.Item(1)) then
  ListView1.ListItems(lvItemScan).ListSubItems.Item(1).ForeColor = vbBlack
End If

Open in new window

BobZZAuthor Commented:
I discovered that I needed to do a ListView1.Refresh after using the above code to change both the ListView ListItem and ListSubItems.

I still need to know how to test to see if there is a ListSubItem before I set the ListSubItems to prevent the code from erroring when there are no ListSubItems associated with a given ListItem.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Use something like:

For i = 1 to ListView1.ListItems(lvItemScan).ListSubItems.Count
   ListView1.ListItems(lvItemScan).ListSubItems.Item(i).ForeColor = vbBlack
BobZZAuthor Commented:
Thanks for the suggestion Paul, however, it was word for word what I had in the example.  As I mentioned in the update I posted, the code works.  The problem I'm left with is some of the ListItems have ListSubItems and some don't.  When the code encounters a ListItem that doesn't have any associated ListSubItems, the code errors off.

What I'm looking for is a way to test each ListItem to see if it has any associated ListSubItems and if so, then update the ListItem but don't try and update the non-existent ListSubItems.


Bob Z.
BobZZAuthor Commented:
Thanks so much.  My appologies, I misread your example the first time.  The solution worked perfectly.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.