Question

I have a custom made listview control that shows a list of notifications to the user. Basically, when a new notification comes in, a new entry is added to the listview in Bold. When the user reads the notification, it turns to regular font.

The only way I could work out how to achieve this probably (a read status) was to use checkboxes. So a new notification would have its item checked and when it was read it was unchecked. This works well and seems to achieve what I need.

However, my question is....is there a way I can remove the drawing of the checkboxes but still keep the functionality in the background. So for example, not draw the checkbox for the listview item, but still be able to use ListView.Checkboxes = True and ListViewItem.Checked = True?

My ListView control is ownerdrawn and the code for my DrawItem event looks like so:

Protected Overrides Sub OnDrawItem(e As DrawListViewItemEventArgs)
    Try
        If Not (e.State And ListViewItemStates.Selected) = 0 Then
            'Draw the background for a selected item.
            e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, e.Bounds)
            e.DrawFocusRectangle()
        Else
            'Draw the background for an unselected item.
            e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Control, e.Bounds)
        End If

        e.DrawBackground()
        e.DrawDefault = True
        MyBase.OnDrawItem(e)

    Catch ex As Exception
        MsgBox("Exception Error: " & ex.Message, MsgBoxStyle.Critical, "Module: lsvOverdueCalls_DrawItem()")
    End Try
End Sub

If I remove the e.DrawDefault = True it removes the checkboxes, but then I can't control the bold face font for a new notification.

Any help appreciated. Thanks

Was it helpful?

Solution

I ended up resolving this by creating a new ListViewItem class which inherits the ListViewItem and then adding a custom property. I then used the new class in referencing a ListViewItem throughout my code which allowed me to add a new attribute to the default ListViewItem.

Public Class cust_ListViewItem
    Inherits ListViewItem

    Private _read As Boolean
    Private RegularFont As New Font(Me.Font.FontFamily, Me.Font.size, FontStyle.Regular)
    Private BoldFont As New Font(Me.Font.FontFamily, Me.Font.size, FontStyle.Bold)

    Public Property Read As Boolean
        Get
            Return _read
        End Get
        Set(value As Boolean)
            _read = value
            MarkAsRead()
        End Set
    End Property

    Private Sub MarkAsRead()
        If _read Then Me.Font = RegularFont Else Me.Font = BoldFont
    End Sub
End Class

Then to call my new property, I used the following:

    Dim lvi As cust_ListViewItem = New cust_ListViewItem
    If Notifications(x).Read = True Then
    lvi.Read = True
    ...

However, I also found the following Link which allows you to completely remove the checkbox from individual listview items which is what I was initially trying to achieve. I just added the code to my custom listview class and applied the code to every listview item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top