|
-
Nov 18th, 2009, 08:13 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Change Listview Backcolor with a condition
Hello ,
i'am trying to make unique backcolor for every listview row
i have some legend 3 with color
Salary > 6000 = Listview row will be blue
Salary > 8000 = Listview row will be pink
Salary > 6000 = Listview row will be green
actually i have 7 column in my Listview
the salary column approrpiate is number 4
what i'am trying to do is :
Code:
If (lvItem.SubItems(4).Text >6000) Then
lvItem.BackColor = Color.CornflowerBlue
lvItem.SubItems(1).BackColor = Color.CornflowerBlue
lvItem.SubItems(2).BackColor = Color.CornflowerBlue
lvItem.SubItems(3).BackColor = Color.CornflowerBlue
lvItem.SubItems(4).BackColor = Color.CornflowerBlue
lvItem.SubItems(5).BackColor = Color.CornflowerBlue
lvItem.SubItems(6).BackColor = Color.CornflowerBlue
lvItem.SubItems(7).BackColor = Color.CornflowerBlue
End If
....
but not work
Thanks
-
Nov 18th, 2009, 09:24 PM
#2
Re: Change Listview Backcolor with a condition
I, instead, would create a unique ListViewItem per object. In this case, you'd make three:
Salary > 6000 = lviBlue
Salary > 8000 = lviPink
Salary > 10000 = lviGreen
Example Usage:
vb Code:
Dim lviBlue As ListViewItem = New ListViewItem lviBlue.Text = "Blue" lviBlue.SubItems.Add("6,000") lviBlue.BackColor = Color.Blue ListView1.Items.Add(lviBlue) Dim lviPink As ListViewItem = New ListViewItem lviPink.Text = "Pink" lviPink.SubItems.Add("8,000") lviPink.BackColor = Color.Pink ListView1.Items.Add(lviPink) Dim lviGreen As ListViewItem = New ListViewItem lviGreen.Text = "Green" lviGreen.SubItems.Add("10,000") lviGreen.BackColor = Color.Green ListView1.Items.Add(lviGreen)
I might be misunderstanding what you're asking for and I, of course, don't know how the rest of your is, so this is my best example usage.
Also, there might be a better way, but this is what I came up with.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 18th, 2009, 09:34 PM
#3
Re: Change Listview Backcolor with a condition
First up, the default value for the UseItemStyleForSubItems property of a ListViewItem is True so, unless you want to use different Fonts or ForeColors for the subitems, you only have to set the item BackColor, not all the subitems too.
Secondly, if you test for greater than 6000 first then everything greater than 8000 and whatever else (you have 6000 twice in your post above) will match too, which you don't want.
vb Code:
Dim backColor As Color Select Case CInt(lvItem.SubItems(4)) Case Is > 9000 backColor = Color.Green Case Is > 8000 backColor = Color.Pink Case Is > 6000 backColor = Color.CornflowerBlue Case Else backColor = SystemColors.Window End Select lvItem.BackColor = backColor
-
Nov 19th, 2009, 06:11 AM
#4
Thread Starter
Fanatic Member
Re: Change Listview Backcolor with a condition
 Originally Posted by jmcilhinney
First up, the default value for the UseItemStyleForSubItems property of a ListViewItem is True so, unless you want to use different Fonts or ForeColors for the subitems, you only have to set the item BackColor, not all the subitems too.
Secondly, if you test for greater than 6000 first then everything greater than 8000 and whatever else (you have 6000 twice in your post above) will match too, which you don't want.
vb Code:
Dim backColor As Color Select Case CInt(lvItem.SubItems(4)) Case Is > 9000 backColor = Color.Green Case Is > 8000 backColor = Color.Pink Case Is > 6000 backColor = Color.CornflowerBlue Case Else backColor = SystemColors.Window End Select lvItem.BackColor = backColor
Thanks John ,
actually there is bug
i modded that code but
Code:
Select Case CInt(item1.SubItems(4).Text)
Case Is > 9000
backColor = Color.Green
Case Is > 8000
backColor = Color.Pink
Case Is > 6000
backColor = Color.Green
Case Else
backColor = SystemColors.CornflowerBlue
End Select
ListView3.BackColor = backColor
actually i'am show listview rows via loop
when i added that code i get the the backcolor changed on the Listview itself
but i'am trying to change backcolor of current row in the listview
@weirddemon,
i do have only 1 Listview
weirddemonz
edit : Fixed now all work Thanks John & weirddemon
Last edited by killer7k; Nov 19th, 2009 at 07:04 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|