如果用户单击树视图(右侧或左侧)并且光标不在节点上,我想将所选节点设置为空。我认为如果单击控件的任何部分,将触发TreeView.MouseClick事件,但似乎只在单击节点时触发。有没有办法做到这一点?
编辑*我知道如何将所选节点设置为空,tvwMain.SelectedNode = Nothing
但是在单击控件时无法执行此操作,并且此时没有节点作为TreeView.MouseClick和TreeView.Click除非单击某个节点,否则事件似乎不会触发。
Private Sub tvwMain_NodeMouseClick(sender As Object, e As MouseEventArgs) Handles tvwMain.Click
Dim p = New Point(e.X, e.Y)
Dim node As TreeNode = tvwMain.GetNodeAt(p)
If node IsNot Nothing Then
tvwMain.SelectedNode = node
MsgBox("HERE")
Else
tvwMain.SelectedNode = Nothing
MsgBox("TOO")
End If
End Sub
答案 0 :(得分:1)
TreeNode test = tvwMain.GetNodeAt(tvwMain.PointToClient(Cursor.Position))
If IsNothing(test) Then tvwMain.SelectedNode = Nothing