【问题标题】:Index of checked/unchecked checkbox in listview列表视图中选中/未选中复选框的索引
【发布时间】:2019-04-02 06:05:00
【问题描述】:

我正在尝试根据列表视图中选中复选框的值(真/假)修改 XML 文件,因此我需要使用的复选框的索引。问题是,每当我选中或取消选中一个复选框时,我在使用 ListView.FocusedItem.Index 时都会得到相同的结果,因为单击复选框时未选中。

是否可以在列表视图中获取当前使用的复选框的索引?

【问题讨论】:

  • 所以不要使用FocusedItem。你为什么会放在第一位? ListView 有一个 CheckedIndices 属性,它为您提供所有选中项的索引,ItemCheckItemChecked 事件都提供对正在或已经被选中或未选中的 ListViewItem 的引用。
  • 没什么可看的,很难回答。我的水晶球说您还没有注意到您可以单击复选框而不也更改所选项目。注意蓝色突出显示。如果您使用 ItemCheck 事件,请务必使用 e.Item 属性。如果您使用 ItemChecked 事件,请务必使用 e.Item.Index 属性。等等。

标签: vb.net listview checkbox indexing


【解决方案1】:

在 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

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多