Results 1 to 3 of 3

Thread: [RESOLVED] Listview Background and Forground Colours for specific sub items.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    21

    Resolved [RESOLVED] Listview Background and Forground Colours for specific sub items.

    Hi, I've trawled a few forums and there is some discussion that the following is either not possible or requires a more complex solution using the graphics capability.

    However as a noob, there appears to be the option (through intellitype) to set the foreground and background colors for the sub items within a Listview.

    The following does not work; is that because I'm doing something silly or it just not that simple ?

    If a value returns TRUE then I'm setting a Text value and trying to color it.

    Code:
     If ValueStatus = True Then
                    LstStatus.Items(Index).SubItems(1).Text = CStr(ValueStatus)
                    LstStatus.Items(Index).SubItems(1).BackColor = Color.Green
                Else
                    LstStatus.Items(Index).SubItems(1).Text = CStr(ValueStatus)
                    LstStatus.Items(Index).SubItems(1).BackColor = Color.LightGray
                End If
    any thoughts out there? thanks, J.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Listview Background and Forground Colours for specific sub items.

    Hi,

    sure you can do that, this will color Items

    Code:
      Dim sDB As String = "E:\Adressen.mdb"
            Dim sCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                    "Data Source=" & sDB & ";"
    
            Dim Cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(sCon)
            Cn.Open()
            'Left([CategoryName],10)
            Dim sSQL As String = "Select ProductName, UnitPrice From Products "
    
          
            Dim Cmd As New OleDb.OleDbCommand(sSQL, Cn)
            Dim Dr As OleDb.OleDbDataReader
            Dr = Cmd.ExecuteReader
    
            With ListView1
                If .Columns.Count = 0 Then
                    .View = View.Details
                    .Columns.Add("ProductName", 150, HorizontalAlignment.Left)
                    .Columns.Add("UnitPrice", 150, HorizontalAlignment.Right)
                End If
                .Items.Clear()
                .BeginUpdate()
    
                Do While Dr.Read
                    Dim Li As New ListViewItem
                    Li.UseItemStyleForSubItems = False
                    Li.Text = Dr(0).ToString
                    Li.SubItems.Add(FormatNumber(Dr("UnitPrice"), 2, TriState.False, TriState.True, TriState.True))
    
    
                    If CDbl(Dr("UnitPrice").ToString) >= 2 AndAlso CDbl(Dr("UnitPrice").ToString) <= 12 Then
                        Li.SubItems(1).ForeColor = Color.AntiqueWhite
                        Li.SubItems(1).BackColor = Color.SaddleBrown
                        Li.ImageIndex = ImageList1.Images.IndexOfKey("isOk")
    
                    ElseIf CDbl(Dr("UnitPrice").ToString) >= 13 AndAlso CDbl(Dr("UnitPrice").ToString) <= 22 Then
                        Li.SubItems(1).ForeColor = Color.Blue
                        Li.ImageIndex = ImageList1.Images.IndexOfKey("notOk")
    
                    ElseIf CDbl(Dr("UnitPrice").ToString) >= 50 Then
                        Li.SubItems(1).ForeColor = Color.Tomato
                    End If
                    .Items.Add(Li)
                Loop
                .EndUpdate()
            End With
            Dr.Close()
    hth
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    21

    Resolved Re: Listview Background and Forground Colours for specific sub items.

    Thanks Chris,
    Ah...yep, I had not turned off the 'UseItemStyleForSubItems' property.

    From my recent simple experience, this needs to be set 'false' when you create each item. from that point on-wards you can modify the style properties of the Sub items.

    Code:
     Dim item0 = New ListViewItem("Power") 
            item0.UseItemStyleForSubItems = False
            item0.SubItems.Add("") 'Value - enter nothing
            item0.SubItems.Add("2") 'Message - depends on extract settings 
            item0.SubItems.Add("Word 4")
            LstValue.Items.Add(item0)
    Works as i need, great, thanks Chris.

Tags for this Thread

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