Highlight or bold part of the test in Listview items and SubItems


I'm displaying a serch results in a listview(windows application) and I need to highlight the searched text within the results. Can anyone pls help?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
You can change the BackColor/ForeColor for a ListViewItem and ListViewItem.ListViewSubItem.

Dim item As ListViewItem = Me.ListView1.Items.Add("Test")
item.BackColor = Color.Red
item.ForeColor = Color.White

Dim subItem As ListViewItem.ListViewSubItem
subItem = item.SubItems.Add("Test 2")
subItem.BackColor = Color.Red
subItem.ForeColor = Color.White


dayandealwisAuthor Commented:
But I want to change only part of the text, not the whole text of the Item or SubItem.
I believe this has to be done by extending the Listview control and using the ownerdraw method. Can anyone help pls.

Bob LearnedCommented:
So, the search text is a sub-part of a ListViewItem.  You gotta start with the easy thing first, and then after finding out the "real" requirement, go from there ;)

Extending the ListView is not a trivial pursuit.  Are you married to the idea of using the ListView, or can you use a ListBox?

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

dayandealwisAuthor Commented:
Yes, it has to be a ListView. It's been intregated to the system and now only this part has to be completed. I sure do appreciate your help. :)

Bob LearnedCommented:

This is what I have so far (incomplete):

Imports System.Runtime.InteropServices
Imports System.Drawing

Public Class ListViewEx
  Inherits System.Windows.Forms.ListView

  <DllImport("User32.dll", CharSet:=CharSet.Auto)> _
  Public Shared Function GetWindowLong(ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Long
  End Function

  <DllImport("User32.dll", CharSet:=CharSet.Auto)> _
  Public Shared Function SetWindowLong(ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
  End Function

  Public Delegate Sub DrawItemEventHandler(ByVal sender As Object, ByVal e As DrawItemEventArgs)
  Public Event DrawItem As DrawItemEventHandler

  Protected Overridable Sub OnDrawItem(ByVal e As DrawItemEventArgs)

    Dim item As ListViewItem = Me.Items(e.Index)
    Dim text As String = item.Text

    If text.Length > 0 Then
      Dim textBrush As New SolidBrush(item.ForeColor)
      Dim fillBrush As New SolidBrush(item.BackColor)
      Dim rect As RectangleF = RectangleF.op_Implicit(e.Bounds)
      e.Graphics.FillRectangle(fillBrush, rect)
      e.Graphics.DrawString(text, e.Font, textBrush, rect)
    End If

  End Sub

  Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    Dim style As Long = GetWindowLong(Me.Handle, -16)
    style = style Or (&H400 Or &H1)
    SetWindowLong(Me.Handle, -16, style)
  End Sub

  <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
  Public Structure DRAWITEMSTRUCT
    Public ctrlType As Integer
    Public ctrlID As Integer
    Public itemID As Integer
    Public itemAction As Integer
    Public itemState As Integer
    Public hwnd As IntPtr
    Public hdc As IntPtr
    Public rcItem As Rectangle
    Public itemData As IntPtr
  End Structure

  Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    Const WM_DRAWITEM As Int32 = &H2B
    Const WM_REFLECT As Int32 = &H2000
    Const ODS_SELECTED As Int32 = &H1

    Select Case m.Msg

        ' Reflected WM_DRAWITEM
        ' Get the DRAWITEMSTRUCT from the LParam of the message
        Dim dis As DRAWITEMSTRUCT = CType(Marshal.PtrToStructure(m.LParam, GetType(DRAWITEMSTRUCT)), DRAWITEMSTRUCT)

        ' Get the graphics from the hdc field of the DRAWITEMSTRUCT
        Dim g As Graphics = Graphics.FromHdc(dis.hdc)

        ' Create new DrawItemState in its default state          
        Dim state As DrawItemState = DrawItemState.Default

        ' Set the correct state for drawing
        If dis.itemState And ODS_SELECTED > 0 Then
          state = DrawItemState.Selected
        End If

        ' Create the DrawItemEventArgs object
        Dim e As New DrawItemEventArgs(g, Me.Font, dis.rcItem, dis.itemID, state)


      Case Else

    End Select

  End Sub

End Class

I doesn't handle bolding part of the ListViewItem.  I'll have to give that some more thought.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dayandealwisAuthor Commented:
Thanks for looking into this Bob. Hope U can come up with the solution soon. Thanks again


dayandealwisAuthor Commented:

One more thing, above code only handles adding of Items. I need to add subItems also. Can U pls tell me how to extend this to add subItems also.



It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.