ListView控件Windows API - CheckAll - CheckCount?

时间:2014-12-16 15:37:12

标签: listview vb6

我有一个VB6项目,我正在使用ListBoxes切换到使用ListViews。列表具有复选框样式。我正在寻找要发送到ListView的Windows API消息,指定:

  • 全部检查
  • 取消全部选中
  • Checked Count

对于ListBox来说这很容易,因为Selected与Checked相同。对于ListView不适用。

这样的事情除了CHECKED not SELECTED:

Private Const LVM_FIRST As Long = &H1000
Private Const LVM_GETITEMCOUNT As Long = (LVM_FIRST + 4)
Private Const LVM_GETSELECTEDCOUNT As Long = (LVM_FIRST + 50)
Private Const LV_SETSEL = &H1000 + 43

Private Function ListViewSelectedCount(ByRef LV As ListView) As Long
    ListViewSelectedCount = SendMessage(LV.hwnd, LVM_GETSELECTEDCOUNT, 0, 0)
End Function

Private Sub ListViewSelectAll(ThisBox As ListView)
    Dim LV As LV_ITEM
    LV.mask = &H8
    LV.State = True
    LV.stateMask = &H2

    Call SendMessage(ThisBox.hwnd, LV_SETSEL, ByVal -1, LV)
End Sub

我找到了ListView消息列表:http://vbnet.mvps.org/index.html?code/comctl/lvmembers.htm 但似乎没有任何我需要的东西。

1 个答案:

答案 0 :(得分:0)

是否有必要使用Windows API消息来设置所选标志。我假设您正在使用Microsoft Windows公共控件中的ListView。如果你那么你可以使用以下代码:

Public Sub SetSelected(ByVal blnCheck As Boolean)
Dim lviItem As ListItem

    For Each lviItem In ListView.ListItems
        lviItem.Checked = blnCheck
    Next

    Set lviItem = Nothing

End Sub

Public Function CountChecked() As Long
Dim lngCount    As Long
Dim lviItem     As ListItem

    For Each lviItem In ListView.ListItems
        If lviItem.Checked Then
            lngCount = lngCount + 1
        End If
    Next

    Set lviItem = Nothing

    CountChecked = lngCount

End Function

使用true或false调用SetSelected并选择/取消选择all,第二个函数将返回计数。您应该将代码的ListView位重命名为ListView的名称。如果你想让它更通用,你可以将listview传递给sub和function,代码看起来像这样:

Public Sub SetSelected(ByRef lvwToChange As ListView, ByVal blnCheck As Boolean)
Dim lviItem As ListItem

    For Each lviItem In lvwToChange.ListItems
        lviItem.Checked = blnCheck
    Next

    Set lviItem = Nothing

End Sub

Public Function CountChecked(ByRef lvwToCount As ListView) As Long
Dim lngCount    As Long
Dim lviItem     As ListItem

    For Each lviItem In lvwToCount.ListItems
        If lviItem.Checked Then
            lngCount = lngCount + 1
        End If
    Next

    Set lviItem = Nothing

    CountChecked = lngCount

End Function

希望这有帮助。