vb.net Treeview - 在 Visual Basic (VS 2012 V11) 中的节点点击事件而不是扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16676093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:40:54  来源:igfitidea点击:

Treeview - Event on Node Click but not Expansion in Visual Basic (VS 2012 V11)

vb.nettreeviewnodesexpandcollapse

提问by user2348797

I'm trying to make a help file in Visual Basic. I've decided to go the route of replicating the old style help files with a TreeViewpanel, to the left, and a RichTextbox, on the right, of the form. (This set-up looks like the help file in PowerShellalmost exactly.

我正在尝试在 Visual Basic 中制作帮助文件。我决定采用在表单TreeView左侧和RichTextbox右侧的面板复制旧式帮助文件的路线。(此设置与PowerShell 中的帮助文件几乎完全一样。

I'm trying to make it so that when a TreeViewNodeis Single Clickedthe RichTextboxTextwill change to the appropriate text. Here is my code:

我试图让这个当TreeViewNodeSingle ClickedRichTextboxText会更改为相应的文字。这是我的代码:

 Private Sub treeView_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeViewContents.NodeMouseClick
        If e.Node.Text.Equals("Program Help") Then
            RTBHelp.Text = Environment.NewLine & "Help text here."
        End If

        If e.Node.Text.Equals("Program Getting Started") Then
            RTBHelp.Text = Environment.NewLine & "Getting Started text here"
        End If

    End Sub

The problem is that the text will change when simply clicking the Plusor Minuslocated next to the TreeViewNode. But, I want to emulate the PowerShellhelp behavior, where clicking the Plusor Minusexpands or collapses the nodes but does not change the RichTextboxText. Only when clicking on the Nodesname (Text) itself should the RichTextboxTextchange. I have tried several methods but none seem to work. What do I do?

问题在于,只需单击.旁边的Plus或,文本就会更改。但是,我想模拟PowerShell帮助行为,其中单击或展开或折叠节点但不会更改. 只有在单击名称 ( ) 本身时才会更改。我尝试了几种方法,但似乎都不起作用。我该怎么办?MinusTreeViewNodePlusMinusRichTextboxTextNodesTextRichTextboxText

回答by stanley chuks

This might be too late but i just had same problem. I used the AfterSelect Event. It is logically that NodeClick Event is fired when one tries to expand the node since we are clicking on the Node by expanding it. If one is interested on just the Selection done by the mouse then it is necessary to check if e.Action = TreeViewAction.ByMouse.

这可能为时已晚,但我只是遇到了同样的问题。我使用了 AfterSelect 事件。从逻辑上讲,当我们尝试扩展节点时会触发 NodeClick 事件,因为我们是通过扩展它来单击节点的。如果只对鼠标完成的选择感兴趣,则有必要检查是否 e.Action = TreeViewAction.ByMouse。

Private Sub treeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeView.AfterSelect
    If e.Action = TreeViewAction.ByMouse Then
        If e.Node.Text.Equals("Program Help") Then
          RTBHelp.Text = Environment.NewLine & "Help text here."
        End If

        If e.Node.Text.Equals("Program Getting Started") Then
            RTBHelp.Text = Environment.NewLine & "Getting Started text here"
        End If
    End If

End Sub

By using "if TreeViewAction.ByMouse then ...", the code under the if Statement will be excuted if one presses the arrow-keys or the mouse. So the first If Statement is very important if only the mouse selection is to be caught.

通过使用“if TreeViewAction.ByMouse then ...”,如果按下方向键或鼠标,将执行if语句下的代码。因此,如果只捕获鼠标选择,则第一个 If 语句非常重要。

回答by DBNickel

Use the AfterSelect event instead.

请改用 AfterSelect 事件。