2016-02-12 74 views
1

我有一个列表框(mainlistbox),它列出指定目录中的文件。当我在列表框中单击它们时,我会在richtextbox中获得该文件的预览。这工作完美,但我想弄清楚如何在列表框中的空白字段中单击鼠标时取消选择mainlistbox中的项目。VB.NET当在空白区域中单击鼠标时取消选择列表框中的项目

Mainlistbox selectedindexchanged确实像文件存在,file.readalltext文本框。

我现在正在用鼠标点击mainlistbox上的事件。

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测试),但我选择了一个之后,我无法取消选择它。 我试过了一堆变体,但我无法让它工作。它可能是超级简单的东西,我还没弄明白。谢谢!

回答

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 
+0

感谢@NDJ,这正是我一直在寻找! :) – MadsTheMan

+0

不客气:) – NDJ