C# 如何在点击事件上获取treeView的节点文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16457702/
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
How to Get Node Text of treeView on the click event
提问by Jiss Sebastian
I want to Get the Text of the node in the treeview. I am using the click()event.. When I am using the AfterSelect()event I can get the node text by e.Node.text. how can I get the text by using Click()event
我想在树视图中获取节点的文本。我正在使用该click()事件.. 当我使用该AfterSelect()事件时,我可以通过e.Node.text. 如何使用Click()事件获取文本
回答by Cody Gray
I don't recommend using the Clickevent for this. The reason is that there are a lotof different places that the user could click on a TreeView control, and many of them do not correspond to an actual node. The AfterSelectevent is a much better choice—it was designed for this usage.
我不建议Click为此使用该事件。原因是用户可以单击 TreeView 控件的不同位置有很多,并且其中许多与实际节点不对应。该AfterSelect事件是一个更好的选择,它是专为这种用法。
Beyond that, the Clickevent is rather difficult to use, because it doesn't provide you very much information in the handler method. It doesn't tell you which button was clicked, where the click event occurred, etc. You have to retrieve all of this information manually. It's recommended that you subscribe to either the MouseClickor the MouseDown/MouseUpevent instead.
除此之外,该Click事件相当难以使用,因为它没有在处理程序方法中为您提供太多信息。它不会告诉您单击了哪个按钮、单击事件发生的位置等。您必须手动检索所有这些信息。我们建议您订阅无论是MouseClick或MouseDown/MouseUp事件来代替。
To figure out what the user clicked on, you need to use the TreeView.HitTestmethod, which returns a TreeViewHitTestInfoobject that contains detailed information about the area where the user clicked, or the somewhat simpler TreeView.GetNodeAtmethod, which will simply return nullif no node exists at the location of the click.
要弄清楚用户点击了什么,您需要使用该TreeView.HitTest方法,该方法返回一个TreeViewHitTestInfo包含有关用户点击区域的详细信息的对象,或者更简单的TreeView.GetNodeAt方法,null如果该位置不存在节点,则该方法将简单地返回点击。
Alternatively, to get the currently selected node at any time, you can just query the TreeView.SelectedNodeproperty. If no node is selected, this will also return null.
或者,要随时获取当前选定的节点,只需查询TreeView.SelectedNode属性即可。如果没有选择节点,这也将返回null。
回答by PawanS
It would be better to use treeView1_AfterSelect()event because that gives the correct selected node text. The treeView1_Click()event will show the oldest selected not, not the immediate selected one.
最好使用treeView1_AfterSelect()事件,因为它给出了正确的选定节点文本。本treeView1_Click()次活动将展示最古老的选择不,不是眼前的选择之一。
You can achieve the selected node text on Clickevent
您可以在Click事件上实现选定的节点文本
private void treeView1_Click(object sender, EventArgs e)
{
MessageBox.Show(treeView1.SelectedNode.Text);
}
Remember, the difference between Click()and AfterSelect()event is their eventargs
请记住,Click()和AfterSelect()event之间的区别在于它们的 eventargs
treeView1_Click(object sender, EventArgs e)
treeView1_AfterSelect(object sender, TreeViewEventArgs e)
EDIT:Try out this on Click()event, I am sure this will help you.
编辑:在Click()活动中试试这个,我相信这会对你有所帮助。
private void treeView1_Click(object sender, EventArgs e)
{
TreeViewHitTestInfo info = treeView1.HitTest(treeView1.PointToClient(Cursor.Position));
if (info != null)
MessageBox.Show(info.Node.Text);
}
回答by Elyh
I found a way which works for me, it took me a while to get to do I wanted but it works.
我找到了一种对我有用的方法,我花了一段时间才去做我想做的事,但它有效。
Private Sub toolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolStripButton7.Click
Dim node As TreeNode = treeView1.SelectedNode
Dim strRootPath As String = My.Settings.DefaultRootPath
Dim strNode As String = treeView1.SelectedNode.Text
Call treeViewRoot(strRootPath)
Dim nodes As TreeNode() = treeView1.Nodes.Find(strRootPath & "\" & strNode, True)
For Each node In nodes
treeView1.Focus()
treeView1.SelectedNode = node
Next
End Sub

