9 Replies - 710 Views - Last Post: 20 September 2018 - 11:06 PM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 16-May 17

How to change the color of a cell(back or fore color) of listview item

Posted 18 September 2018 - 08:13 PM

How to change the color of a cell(back or fore color) of listview item according to item value with if condition.

I do it but it is not work and show no error
Please help me......

What I have tried:

Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddcat.Click
       For k = 0 To ListView1.Items.Count - 1
            If ListView1.Items(k).SubItems(6).Text > 100 Then
                ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
                MsgBox("hi test code yes")
            Else
                MsgBox("hi test code no")
            End If
        Next k
    End Sub



my code it work fine but not change the color of cell

This post has been edited by [email protected]: 18 September 2018 - 08:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to change the color of a cell(back or fore color) of listview item

#2 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,911
  • Joined: 12-December 12

Re: How to change the color of a cell(back or fore color) of listview item

Posted 19 September 2018 - 12:52 AM

The first thing you should correct is to not attempt to compare text to a number (.Text > 100). Add Option Strict On to the top of every module to prevent and highlight type conversion issues.

Then debug and step through your code to check that it actually enters the if-condition at some point.
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 16-May 17

Re: How to change the color of a cell(back or fore color) of listview item

Posted 19 September 2018 - 10:10 AM

View Postandrewsw, on 19 September 2018 - 01:22 PM, said:

The first thing you should correct is to not attempt to compare text to a number (.Text > 100). Add Option Strict On to the top of every module to prevent and highlight type conversion issues.

Then debug and step through your code to check that it actually enters the if-condition at some point.


Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddcat.Click
For k = 0 To ListView1.Items.Count - 1
'If ListView1.Items(k).SubItems(6).Text > 10000 Then
' ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
'End If
Dim value As Integer
If Integer.TryParse(ListView1.Items(k).SubItems(6).Text, value) Then
If value > 10000 Then
ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
End If
End If
Next k
End Sub
It is does not work and not debug the full code, After this line 
" If Integer.TryParse(ListView1.Items(k).SubItems(6).Text, value) Then" go to the 

End If
Next k 
do not debug or read the middle lines.
Please help me sir
Sir Check with my code it work fine but not change the color of cell 
my code is ---
For k = 0 To ListView1.Items.Count - 1
If ListView1.Items(k).SubItems(6).Text > 100 Then
ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
MsgBox("hi test code yes")
Else
MsgBox("hi test code no")
End If
Next k
:code:

This post has been edited by modi123_1: 19 September 2018 - 10:31 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Was This Post Helpful? 0
  • +
  • -

#4 PeterH   User is offline

  • D.I.C Regular

Reputation: 60
  • View blog
  • Posts: 289
  • Joined: 03-September 09

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 06:29 AM

A quick search found This

This post has been edited by modi123_1: 20 September 2018 - 09:04 AM
Reason for edit:: Fixed the double HTTP

Was This Post Helpful? 1
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 16-May 17

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 08:49 AM

View PostPeterH, on 20 September 2018 - 06:59 PM, said:

A quick search found This



This webpage is not available

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,911
  • Joined: 12-December 12

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 10:05 AM

It is available for me, are you blocked from stackoverflow?
Was This Post Helpful? 1
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 108
  • View blog
  • Posts: 760
  • Joined: 05-December 13

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 01:09 PM

Link broke and removed...

This is what you were likely being pointed to:
myListView.Items[0].UseItemStyleForSubItems = false;
myListView.Items[0].SubItems[1].ForeColor = Color.Red;

This post has been edited by Sheepings: 20 September 2018 - 01:11 PM

Was This Post Helpful? 1
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1514
  • View blog
  • Posts: 3,826
  • Joined: 01-February 13

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 01:17 PM

Seems odd that the page would be blocked but, the answer is to set the UseItemStyleForSubItems Property of the ListViewItems to False. Then you can set the ForeColor, BackColor, and Font of the individual SubItems.

If you look at the third example, the VB.Net example at the link I just gave you for the UseItemStyleForSubItems property, it shows how you can change the subitems in this way.
Was This Post Helpful? 1
  • +
  • -

#9 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 108
  • View blog
  • Posts: 760
  • Joined: 05-December 13

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 01:29 PM

Nice to see you again Ray. If you get time, do check your DM :)
Was This Post Helpful? 1
  • +
  • -

#10 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 16-May 17

Re: How to change the color of a cell(back or fore color) of listview item

Posted 20 September 2018 - 11:06 PM

Sir Thanks, thanks to everyone.
All are great. Thanks It is work fine, some forum say me that it is impossible silly question etc. I think I don't know good English and Hugh knowledge on vb.net. OK thanks for replying my silly question and Trying to understand my problem. thanks.
Now my full code is -----


Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddcat.Click
        For k = 0 To ListView1.Items.Count - 1
            If ListView1.Items(k).SubItems(6).Text > 9999 Then
                'This is what you were likely being pointed to: 
                ListView1.Items(k).UseItemStyleForSubItems = False
                ListView1.Items(k).SubItems(6).ForeColor = Color.Red
            Else
                '  MsgBox("hi test code no")
            End If
           
        Next k
End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1