[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?
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.
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?
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
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?
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?
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
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
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.
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
1 Attachment(s)
Re: [Listview] Color subitem's specific text
Quote:
Originally Posted by
ChrisE
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
Attachment 151485
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
Re: [Listview] Color subitem's specific text
Quote:
Originally Posted by
JeezyWonder
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
Re: [Listview] Color subitem's specific text
Hi Jeezy,
did you get it working? jmc already gave you the answer
regards
Chris
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
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
Re: [Listview] Color subitem's specific text
Quote:
Originally Posted by
ChrisE
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
1 Attachment(s)
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.
Attachment 151491
regards
Chris
Re: [Listview] Color subitem's specific text
Quote:
Originally Posted by
ChrisE
Hi,
I tried it, it works on the sample and it worked on a Listview in another project of mine.
Attachment 151491
regards
Chris
THanks, i will try it again tommorow, maybe im missing something :ehh:
ALso does it possbile to change backcolor instead of font color?