Advertisement
Bucher100

Listview search color

Nov 16th, 2022 (edited)
1,830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.73 KB | Source Code | 0 0
  1. Public Class Form2
  2.  
  3.     Private m_sSearchString As String = "Hel"
  4.  
  5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.         ' configure listview...
  7.         Me.ListView1.OwnerDraw = True
  8.         Me.ListView1.View = View.Details
  9.         Me.ListView1.Columns.Add("Column 1")
  10.         Me.ListView1.Columns.Add("Column 2")
  11.         Me.ListView1.Items.Add("Hello World, Hello!!")
  12.         Me.ListView1.Items(0).SubItems.Add("Test")
  13.         Me.ListView1.Items.Add("World, Hello!")
  14.         Me.ListView1.Items(1).SubItems.Add("Test")
  15.     End Sub
  16.  
  17.     Private Sub ListView1_DrawColumnHeader(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) _
  18.         Handles ListView1.DrawColumnHeader
  19.         'no customization required to the column headers...
  20.         e.DrawDefault = True
  21.     End Sub
  22.  
  23.     Private Sub ListView1_DrawSubItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) _
  24.         Handles ListView1.DrawSubItem
  25.         ' draw the background, selected or not...
  26.         If e.Item.Selected And e.ColumnIndex = 0 Then
  27.             e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, e.Bounds)
  28.         Else
  29.             e.DrawBackground()
  30.         End If
  31.         ' draw the text...
  32.         If e.ColumnIndex = 0 And e.SubItem.Text.Contains(m_sSearchString) Then
  33.             ' extract all of the part of the text...
  34.             Dim iCurrentPos As Integer = 0
  35.             Dim sItemText As String = e.SubItem.Text
  36.             Dim iSearchPos As Integer = sItemText.IndexOf(m_sSearchString)
  37.             Dim clsParts As New System.Collections.ArrayList()
  38.             Do Until iSearchPos < 0
  39.                 If iSearchPos > iCurrentPos Then
  40.                     clsParts.Add(sItemText.Substring(iCurrentPos, iSearchPos - 1 - iCurrentPos))
  41.                 End If
  42.                 clsParts.Add(sItemText.Substring(iSearchPos, m_sSearchString.Length))
  43.                 iCurrentPos = iSearchPos + m_sSearchString.Length
  44.                 iSearchPos = sItemText.IndexOf(m_sSearchString, iCurrentPos)
  45.             Loop
  46.             If iSearchPos < sItemText.Length Then
  47.                 clsParts.Add(sItemText.Substring(iCurrentPos))
  48.             End If
  49.             ' write out the text parts one by one...
  50.             Dim clsFont As Font
  51.             Dim iCurrentX As Integer = 0
  52.             Dim clsBrush As Brush
  53.             For Each sPart As String In clsParts
  54.                 ' configure brush and font...
  55.                 If sPart = m_sSearchString Then
  56.                     clsFont = New Font(ListView1.Font, FontStyle.Underline Or FontStyle.Bold)
  57.                     clsBrush = New SolidBrush(System.Drawing.Color.Red)
  58.                 Else
  59.                     clsFont = New Font(ListView1.Font, FontStyle.Regular)
  60.                     clsBrush = New SolidBrush(ListView1.ForeColor)
  61.                 End If
  62.                 ' draw the text...
  63.                 Dim clsBounds As New Rectangle(e.Bounds.X + iCurrentX, e.Bounds.Y, e.Bounds.Width - iCurrentX, e.Bounds.Height)
  64.                 e.Graphics.DrawString(sPart, clsFont, clsBrush, clsBounds)
  65.                 ' get next X position...
  66.                 iCurrentX = iCurrentX + Convert.ToInt32(e.Graphics.MeasureString(sPart, clsFont, _
  67.                     New SizeF(e.Bounds.Width, e.Bounds.Height)).Width)
  68.                 ' release graphics objects...
  69.                 clsBrush.Dispose()
  70.                 clsFont.Dispose()
  71.             Next
  72.         Else
  73.             Dim clsBrush As New SolidBrush(ListView1.ForeColor)
  74.             e.Graphics.DrawString(e.SubItem.Text, ListView1.Font, clsBrush, e.Bounds)
  75.             clsBrush.Dispose()
  76.         End If
  77.     End Sub
  78. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement