2020年5月25日 星期一

vb.net 使用 CheckBox 控制 Listview 的 Checkbox [Checkbox control Listview Checkbox]


1
2
3
    Private Sub lvwLista_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles lvwLista.ItemChecked
        CheckSet(lvwLista, chkSelect)
    End Sub





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
    Private Sub CheckSet(lvw As ListView, chk As CheckBox)
        Dim blnCheck As Boolean = False
        Dim blnUncheck As Boolean = False
        For Each lvwi As ListViewItem In lvwLista.Items
            If lvwi.Checked = True Then
                blnCheck = True
            Else
                blnUncheck = True
            End If
        Next

        If blnCheck = False And blnUncheck = True Then
            chk.CheckState = CheckState.Unchecked
        ElseIf blnCheck = True And blnUncheck = False Then
            chk.CheckState = CheckState.Checked
        Else
            chk.CheckState = CheckState.Indeterminate
        End If
    End Sub



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    Private Sub chkSelect_Click(sender As Object, e As EventArgs) Handles chkSelect.Click
        If chkSelect.Checked = True Then
            For Each lvwi As ListViewItem In lvwLista.Items
                lvwi.Checked = True
            Next
        Else
            For Each lvwi As ListViewItem In lvwLista.Items
                lvwi.Checked = False
            Next
        End If
    End Sub

沒有留言:

張貼留言