我正在尝试基于列表视图中选中复选框的值(是/否)修改XML文件,因此我需要一个已用复选框的索引。问题是,每当我选中或取消选中复选框时,使用ListView.FocusedItem.Index
时都会得到相同的结果,因为未选中单击复选框时。
是否可以获取列表视图中当前使用的复选框的索引?
答案 0 :(得分:0)
在ListView ItemCheck事件中检查e.Index。这是我如何使用此示例。
Private Sub lvCoffee_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles lvCoffee.ItemCheck
' e.Current state - the state before the click
' e.New State - after the click
'While the ListView loads the check boxes, keep the update code from running
If LoadingChecks Then
Exit Sub
End If
'This code runs BEFORE the check mark changes
'Hence the use of e.NewValue
Dim bolFav As Boolean
If e.NewValue = CheckState.Checked Then
bolFav = True
Else
bolFav = False
End If
Cursor.Current = Cursors.WaitCursor
Try
'I store the primary key value in the Tag property of the ListViewItem
DataAccess.UpdateFavorites(CInt(lvCoffee.Items(e.Index).Tag), bolFav)
Catch ex As Exception
MessageBox.Show($"The change to {lvCoffee.Items(e.Index).SubItems(1).Text} failed.{vbCrLf}{ex.Message}", "Favorites change Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Cursor.Current = Cursors.Default
End Try
End Sub