[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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

hi,

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?

Dayan
0
dayandealwis
Asked:
dayandealwis
  • 4
  • 3
1 Solution
 
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

Bob


0
 
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.

Dayan
0
 
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?

Bob
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
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. :)

Dayan
0
 
Bob LearnedCommented:
Dayan,

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)
      textBrush.Dispose()
    End If

  End Sub

  Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    MyBase.OnHandleCreated(e)
    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

      Case WM_DRAWITEM Or WM_REFLECT
        ' 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)
        OnDrawItem(e)

        g.Dispose()

      Case Else
        MyBase.WndProc(m)

    End Select

  End Sub

End Class

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

Bob
0
 
dayandealwisAuthor Commented:
Thanks for looking into this Bob. Hope U can come up with the solution soon. Thanks again

Cheers,

Dayan
0
 
dayandealwisAuthor Commented:
Bob,

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.

Thanks

Cheers,

Dayan
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now