VB.NET在空字段

时间:2016-02-12 11:11:40

标签: .net vb.net

我有一个列表框(mainlistbox),列出指定目录中的文件。当我在列表框中单击它们时,我在richtextbox中预览了该文件。这很好用,但是当我在列表框中的空字段中单击鼠标时,我正在试图弄清楚如何取消选择主列表框中的项目。

Mainlistbox selectedindexchanged就像文件存在,file.readalltext到文本框一样。

我现在正在处理事件,鼠标点击主列表框。

    Private Sub MainListBox_MouseClick(sender As Object, e As MouseEventArgs) Handles MainListBox.MouseClick

    If (MainListBox.SelectedIndex = -1) Then 'This is where I've tried "everything"
        MainListBox.ClearSelected()
end if

enter image description here

在我选择一个项目(使用msgbox测试)之前似乎有效,但是在我选择了一个之后,我就无法取消选择它。 我尝试了很多变种,但我无法让它发挥作用。它可能是一些非常简单的东西,我还没想到。谢谢!

1 个答案:

答案 0 :(得分:3)

如果单击某个项目,您可以检查鼠标,如果没有清除您的数据:

Private Sub MainListBox_MouseUp(sender As Object, e As MouseEventArgs) Handles MainListBox.MouseUp
If MainListBox.IndexFromPoint(e.Location) < 0 Then
  ' clear the data
End If
End Sub