ListView Forecolor=Red when number in box is between...

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have a textbox on my form, a button, and a listview (viewed by details so its a grid).

The user enters a number in the textbox and click the button (calculate) and it then shows information in my listview.

In the listview one of the columns it has is "Level" and it will show the levels required to do things. So is there a way that I can have the listview change the item rows to red if the number under "Level" is higher than what they entered in the textbox?
 
I'm going to loop through the listview looking for a value in subitems(?). If the value is more than the textbox's entered value then make the listview line red. I'm using the values as Intergers with CInt. I also changed the listview lines to black if the entered value is less than the entered textbox value.

Visual Basic:
Dim i as Integer
For i = 0 to ListView1.Items.Count - 1
    If CInt(ListView1.Items(i).SubItems(?).Text) > CInt(TextBox1.Text) Then
        ListView1.Items(i).SubItems(0).ForeColor = Color.Red
    Else : ListView1.Items(i).SubItems(0).ForeColor = Color.Black
    End If
Next

With another line of code you can also make only the listview cell red.

Visual Basic:
Dim i as Integer
For i = 0 to ListView1.Items.Count - 1
    ListView1.Items(i).UseItemStyleForSubItems = False
    If CInt(ListView1.Items(i).SubItems(?).Text) > CInt(TextBox1.Text) Then
        ListView1.Items(i).SubItems(5).ForeColor = Color.Red 'the listview cell @ #5
    Else : ListView1.Items(i).SubItems(0).ForeColor = Color.Black
    End If
Next
 
Back
Top