3
votes

I have a TreeView control. Say I have 5 nodes in it. On selecting a node, I populate a ListView with all the Directories under that SelectedNode. Then, I click a Button, which modifies the ListView Items.

So, when I select the Node in the TreeView again (the same one which I selected previously), the AfterSelect event is not firing. And because of this my ListView is not getting updated.

Any ideas guys/gals?

5
Expecting the user to discover that clicking a node that is already selected will have an effect is wishful thinking. Add a Refresh button and implement the F5 keystroke.Hans Passant

5 Answers

2
votes

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;
        }
    };
1
votes

This is not possible. The AfterSelect event will not be raised again, because the node that was selected is already selected. 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. 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.

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. 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. 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
        // . . . 
    }
}
0
votes

It's not firing because the item is already selected. Handle the MouseDown or PreviewMouseDown instead.

0
votes

You can use the MouseClick event instead of the AfterSelect event:

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
-1
votes

@Cody Gray Even this is a very all post, just like to post and answer. 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()