行单击时列表视图选中/取消选中复选框

时间:2018-03-22 14:21:47

标签: vb.net listview checkbox

当我点击该行时,我想检查或取消选中列表视图中的复选框。

1 个答案:

答案 0 :(得分:0)

假设这是WinForms而你有ListView控件的FullRowSelect = true,你可以使用控件的MouseDown事件找到被点击的行:

void listView1_MouseDown(object sender, MouseEventArgs e) {
  var lvi = listView1.GetItemAt(e.X, e.Y);
  if (lvi != null) {
    lvi.Checked = !lvi.Checked;
  }
}

VB.Net:

Private Sub ListView1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDown
  Dim lvi = ListView1.GetItemAt(e.X, e.Y)
  If lvi IsNot Nothing Then
    lvi.Checked = Not lvi.Checked
  End If
End Sub