简体   繁体   English

多次触发 TreeView AfterSelect 事件

[英]Trigger TreeView AfterSelect event multiple times

I have a TreeView control.我有一个 TreeView 控件。 Say I have 5 nodes in it.假设我有 5 个节点。 On selecting a node, I populate a ListView with all the Directories under that SelectedNode.在选择一个节点时,我用该 SelectedNode 下的所有目录填充 ListView。 Then, I click a Button, which modifies the ListView Items.然后,我单击一个 Button,它修改 ListView 项目。

So, when I select the Node in the TreeView again (the same one which I selected previously), the AfterSelect event is not firing.因此,当我再次选择 TreeView 中的节点(与我之前选择的节点相同)时,AfterSelect 事件不会触发。 And because of this my ListView is not getting updated.正因为如此,我的 ListView 没有得到更新。

Any ideas guys/gals?任何想法家伙/女孩?

A workaround is to toggle the selected node...一种解决方法是切换选定的节点...

    treeView.NodeMouseClick += delegate(object sender, TreeNodeMouseClickEventArgs e) {
        TreeNode selected = e.Node;

        // If node already selected - unselect, then reselect
        if (selected == treeView.SelectedNode) {
            treeView.SelectedNode = null;
            treeView.SelectedNode = selected;
        }
    };

This is not possible.这不可能。 The AfterSelect event will not be raised again, because the node that was selected is already selected. AfterSelect事件将不会再次引发,因为选择的节点被选中。 The selection is not changing , so the event will not be raised.选择不会改变,所以不会引发事件。

As Hans points out in a comment to the original question, it's very likely poor UI design to expect a user to realize that clicking again on a node that is already selected will have some sort of effect.正如 Hans 在对原始问题的评论中指出的那样,期望用户意识到再次单击已选择的节点会产生某种效果的 UI 设计很可能很糟糕。 The better solution is to add "Refresh" functionality to your application.更好的解决方案是向您的应用程序添加“刷新”功能。 This is generally mapped to the F5 key, and/or the Ctrl + R keyboard shortcut.这通常映射到F5键和/或Ctrl + R键盘快捷键。

If you absolutely must trigger some action when a node is re-selected , you will need to handle it at a lower level than the AfterSelect event.如果在重新选择节点时绝对必须触发某些操作,则需要在比AfterSelect事件更低的级别处理它。 And that means figuring out which node the user clicked manually.这意味着弄清楚用户手动单击了哪个节点。 To do that, handle the MouseDown event , and use the HitTest method to determine the node at the location the user clicked.为此,请处理MouseDown事件,并使用HitTest方法确定用户单击位置处的节点。 It's not pretty, nor do I recommend it, but it will get the job done.它不漂亮,我也不推荐它,但它可以完成工作。

private void myTreeView_MouseDown(object sender, MouseEventArgs e)
{
    TreeViewHitTestInfo info = myTreeView.HitTest(e.X, e.Y);

    // Ensure that the user actually clicked on a node (there are lots of areas
    // over which they could potentially click that do not contain a node)
    if ((info.Node != null) && (info.Node == myTreeView.SelectedNode))
    {
        // The user clicked on the currently-selected node,
        // so refresh the TreeView
        // . . . 
    }
}

You can use the MouseClick event instead of the AfterSelect event:您可以使用 MouseClick 事件代替 AfterSelect 事件:

Sub treeView1_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick

    ' textBox1.Text = e.Node.Text
    If Not e.Node.Tag Is Nothing Then
        Dim frm As Form = DirectCast(e.Node.Tag, Form)
        frm.ShowDialog()
        ''frm.Dispose()

    End If

End Sub

It's not firing because the item is already selected.它不会触发,因为该项目已被选中。 Handle the MouseDown or PreviewMouseDown instead.改为处理MouseDownPreviewMouseDown

@Cody Gray Even this is a very all post, just like to post and answer. @Cody Gray 即使这是一个非常全面的帖子,只是喜欢发帖和回答。 I think if you Collapse & Expand the treeview in the same time you select node it will work.我认为如果您在选择节点的同时折叠并展开树视图,它将起作用。 codes as below代码如下

MyTreeview.CollapseAll()
MyTreeview.ExpandAll()

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

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