简体   繁体   English

VB.NET在空字段中单击鼠标时取消选中列表框中的项目

[英]VB.NET Unselect item in listbox when mouse click in empty field

I have a listbox ( mainlistbox ) that lists files in a specified directory. 我有一个列表框( mainlistbox ),列出指定目录中的文件。 When I click them in the listbox I get a preview of the file in a richtextbox. 当我在列表框中单击它们时,我在richtextbox中预览了该文件。 This works perfectly, but I'm trying to figure out how to unselect an item in the mainlistbox when I mouse click in a empty field in the listbox. 这很好用,但是当我在列表框中的空字段中单击鼠标时,我正在试图弄清楚如何取消选择主列表框中的项目。

Mainlistbox selectedindexchanged does thing like if file exists, file.readalltext to the textbox. 如果文件存在,则将文件Mainlistbox selectedindexchanged ,如果文件存在,则将文件框格式化为文件框。

I'm now working on the event for mouse click on 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

在此输入图像描述

It seems to work before I got a item selected (tested using msgbox), but after I've selected one, I just can't unselect it. 它似乎在我选择一个项目之前工作(使用msgbox测试),但在我选择了一个之后,我就是无法取消选择它。 I've tried a bunch of variations, but I can't get it to work. 我尝试了很多变种,但我无法让它发挥作用。 Its probably something super simple that I've just not figure out yet. 它可能是一些非常简单的东西,我还没想到。 Thanks! 谢谢!

you can check on the mouse up if an item has been clicked, and if not clear your data: 如果某个项目被点击,您可以检查鼠标,如果没有清除您的数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM