Results 1 to 4 of 4

Thread: [RESOLVED] Change Listview Backcolor with a condition

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Resolved [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

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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:
    1. Dim lviBlue As ListViewItem = New ListViewItem
    2.         lviBlue.Text = "Blue"
    3.         lviBlue.SubItems.Add("6,000")
    4.         lviBlue.BackColor = Color.Blue
    5.         ListView1.Items.Add(lviBlue)
    6.  
    7.         Dim lviPink As ListViewItem = New ListViewItem
    8.         lviPink.Text = "Pink"
    9.         lviPink.SubItems.Add("8,000")
    10.         lviPink.BackColor = Color.Pink
    11.         ListView1.Items.Add(lviPink)
    12.  
    13.         Dim lviGreen As ListViewItem = New ListViewItem
    14.         lviGreen.Text = "Green"
    15.         lviGreen.SubItems.Add("10,000")
    16.         lviGreen.BackColor = Color.Green
    17.         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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim backColor As Color
    2.  
    3. Select Case CInt(lvItem.SubItems(4))
    4.     Case Is > 9000
    5.         backColor = Color.Green
    6.     Case Is > 8000
    7.         backColor = Color.Pink
    8.     Case Is > 6000
    9.         backColor = Color.CornflowerBlue
    10.     Case Else
    11.         backColor = SystemColors.Window
    12. End Select
    13.  
    14. lvItem.BackColor = backColor
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Change Listview Backcolor with a condition

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Dim backColor As Color
    2.  
    3. Select Case CInt(lvItem.SubItems(4))
    4.     Case Is > 9000
    5.         backColor = Color.Green
    6.     Case Is > 8000
    7.         backColor = Color.Pink
    8.     Case Is > 6000
    9.         backColor = Color.CornflowerBlue
    10.     Case Else
    11.         backColor = SystemColors.Window
    12. End Select
    13.  
    14. 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
  •  



Click Here to Expand Forum to Full Width