Results 1 to 18 of 18

Thread: [Listview] Color subitem's specific text

  1. #1

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Question [Listview] Color subitem's specific text

    I Want to check subitems' text property with string var property and if those do not matches, highlight the non-matching text in listview1.subitem
    Code:
    dim data as string
    'got string value in data
    If founditem.SubItems(2).Text <> data then         'checking them
       'if true, highlight not equal text in lv1.subitem with yellow
      End if
    Is it possible?

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

    Re: [Listview] Color subitem's specific text

    Yes it's possible and it's very easy to find out that that's the case and how to do it. VS has a Help menu for a reason. If you had used the Help menu to open the documentation, you could have read about the ListViewItem.ListViewSubItem class and the property that is relevant to this question. I suggest that you do that now.
    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

  3. #3

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    Oh really? all i found is "
    With a standard ListView and ListViewItem cannot have multiple fonts/colours in a single item" SO its not so easy, huh?

  4. #4

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    Btw, if you are talking about Back Color or Font Color properties, then yes its easy.
    Code:
    ListView1.Items.Add("b")
     ListView1.Items.Add("c")
    
        For Each itm As ListViewItem In ListView1.Items
            If itm.Text <> "c" Then
                itm.BackColor = Color.Red
            End If
        Next
    but the problem that i need to highlight only the text then is not equal other then whole line of a subitem.
    , for exmaple 55 55 55 55 55 - already in listbox in third column(subitems(2)) and data as string contains 55 55 55 55 AA, so what i want its highlight only last 55, becaouse only this is diffrent

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

    Re: [Listview] Color subitem's specific text

    If that's all you found then you didn't look very hard. Have you read the documentation for the ListViewItem.ListViewSubItem class? If not, why not?
    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

  6. #6

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    My excuses,
    Typically, the styles of the item and the subitems are the same in a ListView control, but if you want to change the style of a specific ListViewItem.ListViewSubItem to highlight it, you can use these properties on the items you want to display differently.
    But again any simplest sample hot accomlish it?

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

    Re: [Listview] Color subitem's specific text

    Hi,

    is it this your after..
    Code:
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            Dim x As Int32
            For x = 0 To ListView1.Items.Count - 1
                If ListView1.Items(x).SubItems(1).Text.Contains("Mutton") Then
                    ListView1.Items(x).SubItems(1).ForeColor = Color.Blue
                Else
                    ListView1.Items(x).SubItems(1).ForeColor = Color.Black
                End If
            Next
        End Sub
    regards
    Chris
    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.

  8. #8

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    Thx for reply, it's close but it colors whole text in subitems, but it should color only the text is differ... like this;
    55 55 55 55 55 - already in listbox in third column(subitems(2)) and data as string contains 55 55 55 55 AA,
    55 55 55 55 55 last only highlighted in listview subitems text becouse only this was differ

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

    Re: [Listview] Color subitem's specific text

    Ah OK, I misunderstood the question. I though that you wanted to colour specific subitems, not specific text within subitems. My apologies. You would have to owner-draw the items for that. So, possible but not simple.
    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

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

    Re: [Listview] Color subitem's specific text

    Hi
    I found this sample(not my Code)


    Code:
    Public Class Form2
    
        Private m_sSearchString As String = "Hel"
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' configure listview...
            Me.ListView1.OwnerDraw = True
            Me.ListView1.View = View.Details
            Me.ListView1.Columns.Add("Column 1")
            Me.ListView1.Columns.Add("Column 2")
            Me.ListView1.Items.Add("Hello World, Hello!!")
            Me.ListView1.Items(0).SubItems.Add("Test")
            Me.ListView1.Items.Add("World, Hello!")
            Me.ListView1.Items(1).SubItems.Add("Test")
        End Sub
    
        Private Sub ListView1_DrawColumnHeader(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) _
            Handles ListView1.DrawColumnHeader
            'no customization required to the column headers...
            e.DrawDefault = True
        End Sub
    
        Private Sub ListView1_DrawSubItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) _
            Handles ListView1.DrawSubItem
            ' draw the background, selected or not...
            If e.Item.Selected And e.ColumnIndex = 0 Then
                e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, e.Bounds)
            Else
                e.DrawBackground()
            End If
            ' draw the text...
            If e.ColumnIndex = 0 And e.SubItem.Text.Contains(m_sSearchString) Then
                ' extract all of the part of the text...
                Dim iCurrentPos As Integer = 0
                Dim sItemText As String = e.SubItem.Text
                Dim iSearchPos As Integer = sItemText.IndexOf(m_sSearchString)
                Dim clsParts As New System.Collections.ArrayList()
                Do Until iSearchPos < 0
                    If iSearchPos > iCurrentPos Then
                        clsParts.Add(sItemText.Substring(iCurrentPos, iSearchPos - 1 - iCurrentPos))
                    End If
                    clsParts.Add(sItemText.Substring(iSearchPos, m_sSearchString.Length))
                    iCurrentPos = iSearchPos + m_sSearchString.Length
                    iSearchPos = sItemText.IndexOf(m_sSearchString, iCurrentPos)
                Loop
                If iSearchPos < sItemText.Length Then
                    clsParts.Add(sItemText.Substring(iCurrentPos))
                End If
                ' write out the text parts one by one...
                Dim clsFont As Font
                Dim iCurrentX As Integer = 0
                Dim clsBrush As Brush
                For Each sPart As String In clsParts
                    ' configure brush and font...
                    If sPart = m_sSearchString Then
                        clsFont = New Font(ListView1.Font, FontStyle.Underline Or FontStyle.Bold)
                        clsBrush = New SolidBrush(System.Drawing.Color.Red)
                    Else
                        clsFont = New Font(ListView1.Font, FontStyle.Regular)
                        clsBrush = New SolidBrush(ListView1.ForeColor)
                    End If
                    ' draw the text...
                    Dim clsBounds As New Rectangle(e.Bounds.X + iCurrentX, e.Bounds.Y, e.Bounds.Width - iCurrentX, e.Bounds.Height)
                    e.Graphics.DrawString(sPart, clsFont, clsBrush, clsBounds)
                    ' get next X position...
                    iCurrentX = iCurrentX + Convert.ToInt32(e.Graphics.MeasureString(sPart, clsFont, _
                        New SizeF(e.Bounds.Width, e.Bounds.Height)).Width)
                    ' release graphics objects...
                    clsBrush.Dispose()
                    clsFont.Dispose()
                Next
            Else
                Dim clsBrush As New SolidBrush(ListView1.ForeColor)
                e.Graphics.DrawString(e.SubItem.Text, ListView1.Font, clsBrush, e.Bounds)
                clsBrush.Dispose()
            End If
        End Sub
    End Class
    regards
    Chris
    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.

  11. #11

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Arrow Re: [Listview] Color subitem's specific text

    Quote Originally Posted by ChrisE View Post
    Hi
    I found this sample(not my Code)


    Code:
    Public Class Form2
    
        Private m_sSearchString As String = "Hel"
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' configure listview...
            Me.ListView1.OwnerDraw = True
            Me.ListView1.View = View.Details
            Me.ListView1.Columns.Add("Column 1")
            Me.ListView1.Columns.Add("Column 2")
            Me.ListView1.Items.Add("Hello World, Hello!!")
            Me.ListView1.Items(0).SubItems.Add("Test")
            Me.ListView1.Items.Add("World, Hello!")
            Me.ListView1.Items(1).SubItems.Add("Test")
        End Sub
    
        Private Sub ListView1_DrawColumnHeader(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) _
            Handles ListView1.DrawColumnHeader
            'no customization required to the column headers...
            e.DrawDefault = True
        End Sub
    
        Private Sub ListView1_DrawSubItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) _
            Handles ListView1.DrawSubItem
            ' draw the background, selected or not...
            If e.Item.Selected And e.ColumnIndex = 0 Then
                e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, e.Bounds)
            Else
                e.DrawBackground()
            End If
            ' draw the text...
            If e.ColumnIndex = 0 And e.SubItem.Text.Contains(m_sSearchString) Then
                ' extract all of the part of the text...
                Dim iCurrentPos As Integer = 0
                Dim sItemText As String = e.SubItem.Text
                Dim iSearchPos As Integer = sItemText.IndexOf(m_sSearchString)
                Dim clsParts As New System.Collections.ArrayList()
                Do Until iSearchPos < 0
                    If iSearchPos > iCurrentPos Then
                        clsParts.Add(sItemText.Substring(iCurrentPos, iSearchPos - 1 - iCurrentPos))
                    End If
                    clsParts.Add(sItemText.Substring(iSearchPos, m_sSearchString.Length))
                    iCurrentPos = iSearchPos + m_sSearchString.Length
                    iSearchPos = sItemText.IndexOf(m_sSearchString, iCurrentPos)
                Loop
                If iSearchPos < sItemText.Length Then
                    clsParts.Add(sItemText.Substring(iCurrentPos))
                End If
                ' write out the text parts one by one...
                Dim clsFont As Font
                Dim iCurrentX As Integer = 0
                Dim clsBrush As Brush
                For Each sPart As String In clsParts
                    ' configure brush and font...
                    If sPart = m_sSearchString Then
                        clsFont = New Font(ListView1.Font, FontStyle.Underline Or FontStyle.Bold)
                        clsBrush = New SolidBrush(System.Drawing.Color.Red)
                    Else
                        clsFont = New Font(ListView1.Font, FontStyle.Regular)
                        clsBrush = New SolidBrush(ListView1.ForeColor)
                    End If
                    ' draw the text...
                    Dim clsBounds As New Rectangle(e.Bounds.X + iCurrentX, e.Bounds.Y, e.Bounds.Width - iCurrentX, e.Bounds.Height)
                    e.Graphics.DrawString(sPart, clsFont, clsBrush, clsBounds)
                    ' get next X position...
                    iCurrentX = iCurrentX + Convert.ToInt32(e.Graphics.MeasureString(sPart, clsFont, _
                        New SizeF(e.Bounds.Width, e.Bounds.Height)).Width)
                    ' release graphics objects...
                    clsBrush.Dispose()
                    clsFont.Dispose()
                Next
            Else
                Dim clsBrush As New SolidBrush(ListView1.ForeColor)
                e.Graphics.DrawString(e.SubItem.Text, ListView1.Font, clsBrush, e.Bounds)
                clsBrush.Dispose()
            End If
        End Sub
    End Class
    regards
    Chris
    Chis thanks a lot, it is what i was looking for
    Name:  ??????????.png
Views: 3276
Size:  4.3 KB
    but the problem is that its search text in 0 index(column 1). I cant figure out what i should change in code to make it search in index 1(column 2). Any help, please

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

    Re: [Listview] Color subitem's specific text

    Quote Originally Posted by JeezyWonder View Post
    I cant figure out what i should change in code to make it search in index 1(column 2). Any help, please
    Maybe specify an index of 1 instead of an index of 0. Did you actually read the code?
    Code:
    If e.ColumnIndex = 0 And e.SubItem.Text.Contains(m_sSearchString) Then
    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

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

    Re: [Listview] Color subitem's specific text

    Hi Jeezy,

    did you get it working? jmc already gave you the answer

    regards
    Chris
    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.

  14. #14

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    jmcilhinney , Of cause i tried it, Sorry, I'm not soo stupid as you think It doesnt work
    Hi Chris, no changing index actually do nothing, i changed:
    Code:
    Private m_sSearchString As String = "Tes"
    and
    Code:
    If e.ColumnIndex = 1 And e.SubItem.Text.Contains(m_sSearchString) Then
    . It doesnt color anything
    Last edited by JeezyWonder; Sep 6th, 2017 at 05:33 AM.

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

    Re: [Listview] Color subitem's specific text

    Hi Jeezy,

    are you seraching in your listview now ?
    did you set your listview to..
    Code:
     Me.ListView1.OwnerDraw = True
    regards
    Chris
    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.

  16. #16

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    Quote Originally Posted by ChrisE View Post
    Hi Jeezy,

    are you seraching in your listview now ?
    did you set your listview to..
    Code:
     Me.ListView1.OwnerDraw = True
    regards
    Chris
    No, im testing code you gave me. You can try out by yourself, if you want of cause. But Changing ColumnIndex doesnt help

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

    Re: [Listview] Color subitem's specific text

    Hi,

    I tried it, it works on the sample and it worked on a Listview in another project of mine.


    Name:  ColorListviewText.JPG
Views: 2897
Size:  11.1 KB


    regards
    Chris
    Last edited by ChrisE; Sep 6th, 2017 at 06:10 AM.
    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.

  18. #18

    Thread Starter
    Junior Member JeezyWonder's Avatar
    Join Date
    Jul 2017
    Posts
    28

    Re: [Listview] Color subitem's specific text

    Quote Originally Posted by ChrisE View Post
    Hi,

    I tried it, it works on the sample and it worked on a Listview in another project of mine.


    Name:  ColorListviewText.JPG
Views: 2897
Size:  11.1 KB


    regards
    Chris
    THanks, i will try it again tommorow, maybe im missing something
    ALso does it possbile to change backcolor instead of font color?

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