需要从OwnerDrawn列表视图中删除复选框但仍保留复选框功能

时间:2014-04-26 00:11:57

标签: vb.net listview checkbox ownerdrawn

我有一个自定义列表视图控件,显示给用户的通知列表。基本上,当新通知进入时,新条目将添加到Bold中的列表视图中。当用户阅读通知时,它将变为常规字体。

我可以解决如何实现这一目标的唯一方法(读取状态)是使用复选框。因此,新的通知将检查其项目,并在读取它时取消选中。这很好用,似乎达到了我的需要。

然而,我的问题是....有没有办法可以删除复选框的图纸,但仍然保留功能在后台。例如,不是为listview项绘制复选框,但仍然可以使用ListView.Checkboxes = True和ListViewItem.Checked = True?

我的ListView控件是ownerdrawn,我的DrawItem事件的代码如下所示:

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

如果我删除e.DrawDefault = True,则会删除复选框,但我无法控制粗体字体以获取新通知。

任何帮助表示赞赏。 感谢

1 个答案:

答案 0 :(得分:0)

我最终通过创建一个新的ListViewItem类来解决这个问题,该类继承了ListViewItem,然后添加了一个自定义属性。然后我在整个代码中使用新类引用ListViewItem,这允许我将新属性添加到默认的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

然后调用我的新属性,我使用了以下内容:

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

但是,我还发现了以下Link,它允许您从单个列表视图项中完全删除复选框,这是我最初尝试实现的。我刚刚将代码添加到自定义listview类中,并将代码应用于每个listview项目。