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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

For i = 1 to ListView1.ListItems(lvItemScan).ListSubItems.Count
   ListView1.ListItems(lvItemScan).ListSubItems.Item(i).ForeColor = vbBlack
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobZZAuthor Commented:
Thanks so much.  My appologies, I misread your example the first time.  The solution worked perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.