Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys need help.

I created a listview with 2 column.
Name and Score

I need to change the color of those scores below 85% below is my code that i found in google but its not working properly

VB
    Private Sub ListView1_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
        e.DrawDefault = True
    End Sub

    Private Sub ListView1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles ListView1.DrawItem
        Dim ColIndex As Integer = 6
        e.Item.UseItemStyleForSubItems = False

        Dim si As ListViewItem.ListViewSubItem = e.Item.SubItems(2)
' textbox1.text has a value of 85%
        If si.Text > Me.TextBox1.Text Then
            si.ForeColor = Color.Black
        Else
            si.ForeColor = Color.Red
        End If
        e.DrawDefault = True
    End Sub

    Private Sub ListView1_DrawSubItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
        e.DrawDefault = True
    End Sub
Posted
Updated 17-Sep-14 15:57pm
v2
Comments
Richard MacCutchan 18-Sep-14 4:15am    
I don't think you can compare text with the > operator.

I change my code to:

VB
For Each lvi As ListViewItem In Me.ListView1.Items
      Dim va As String = 89%
      If lvi.SubItems(2).Text < va Then

          lvi.UseItemStyleForSubItems = False
          lvi.SubItems(2).ForeColor = Color.red
      End If

  Next


However everytime I get 100% the value still change to red :(
 
Share this answer
 
Comments
F. Xaver 18-Sep-14 4:43am    
still, like Richard MacCutchan sayed.. you can't use < or > operators on Strings in this way..
you need your current % value and your 89% as some numeric Type.... than

dim yourListviewValue as integer = 73
if yourListviewValue < 89 then 'this will work.
Error msg. Coversion string "87%"(value of lisvtview) to type double is not valid.
 
Share this answer
 
Comments
Richard MacCutchan 18-Sep-14 5:51am    
Maybe it is time to go back to the documentation and learn the rules of VB.NET.
Member 11088283 18-Sep-14 5:55am    
I actually changed the color of the value less that 89% however if the value is 100% it also changed the color :(
ChauhanAjay 18-Sep-14 22:56pm    
Have you tried removing the percentage and converting both the values to double or integer and the compare it and change the color.

Hope this helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900