Link to home
Start Free TrialLog in
Avatar of BobZZ
BobZZ

asked on

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

Avatar of BobZZ
BobZZ

ASKER

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

Avatar of BobZZ

ASKER

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.
Use something like:

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

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BobZZ

ASKER

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