We help IT Professionals succeed at work.

Change ListView SubItem Forecolor at runtime

BobZZ
BobZZ asked
on
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

Comment
Watch Question

Author

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

Author

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.

Commented:
Use something like:

For i = 1 to ListView1.ListItems(lvItemScan).ListSubItems.Count
   ListView1.ListItems(lvItemScan).ListSubItems.Item(i).ForeColor = vbBlack
Next

Author

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.

Regards,

Bob Z.
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.

Author

Commented:
Thanks so much.  My appologies, I misread your example the first time.  The solution worked perfectly.