0

我相信这很容易,但我现在遇到了这个问题。我有一个列表视图,每次单击一个项目时,我都会将其名称存储在一个字符串变量中。发生的情况是,当我单击列表视图上的一个项目,然后再次单击列表视图内部而不选择任何项目时,它不会失去焦点并将前一个选定项目的名称存储在字符串变量中。我想要发生的是,每次我单击一个项目,然后单击列表视图内部或外部而不选择任何项目时,它必须失去焦点并且不将项目名称存储在字符串变量中。

Private Sub lvReceivedFiles_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles lvReceivedFiles.MouseClick

    If e.Button = Windows.Forms.MouseButtons.Left Then

        If lvRecievedFiles.FocusedItem.Selected = True

              lvReceivedFiles.FullRowSelect = True

              'When an item is clicked in the list view, store its name.
              fName = "\" & Path.GetFileName(lvReceivedFiles.FocusedItem.Text)

        Else

              'This part here doesn't make any sense because it doesn't execute
              'every time I click in the list view without selecting any item.
              'ListView.FocusedItem.Selected is always true.

        End If

    End If

End Sub
4

2 回答 2

0

处理这种情况的更好方法是使用 SelectedIndexChanged 事件而不是 MouseClick 事件。检查此代码。

Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvReceivedFiles.SelectedIndexChanged

If lvRecievedFiles.FocusedItem.Selected = True

          lvReceivedFiles.FullRowSelect = True

          'When an item is clicked in the list view, store its name.
          fName = "\" & Path.GetFileName(lvReceivedFiles.FocusedItem.Text)

Else

          'This part here doesn't make any sense because it doesn't execute
          'every time I click in the list view without selecting any item.
          'ListView.FocusedItem.Selected is always true.

End If

End Sub
于 2013-02-11T09:46:19.000 回答
0

你可以在MouseUp event.

Private Sub lvlcheckin_MouseUp(sender As Object, e As MouseEventArgs) Handles lvlcheckin.MouseUp
    If e.Button = Windows.Forms.MouseButtons.Left Then

        If lvlcheckin.FocusedItem.Selected = True Then
            lvlcheckin.FullRowSelect = True
        Else
            lvlcheckin.FullRowSelect = False
        End If
    End If
End Sub
于 2013-02-11T09:51:20.680 回答