Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement