שתף באמצעות


Treeview events - issue with Selected Node firing DoubleClick

Question

Thursday, April 21, 2016 3:38 PM

Hi
*
A bit of a conundrum here. I have a Treeview which displays everything I want perfectly. Everything works as I want, with one exception: When I double click on a 'folder' node to open list, it fills the child nodes fine, *but* it will automatically select the first child node - as I have a node double click event handler, this fires and runs the code involved. I have had to add what I will call a 'kludge' to avoid the node double click event running until user actually clicks on a child node.
*
I have tried setting TreeView1.SelectedNode = e.Node in the TreeView1_AfterExpand handler, but that doesn't intercept the DoubleClick event, so doesn't stop the event from firing.
*
My 'kludge' is the use of a boolean variable (skipnode) which I set to True in the AfterExpand, and this bypasses the 'first' hit of the DoubleClick handler.
*
I can't help but think I have missed something obvious that overcomes this issue - anyone shed some light?
*
Code to show KLUDGE:

    ' KLUDGE added to avoid node dbl click event executing
    Dim skipnode As Boolean = True

    Private Sub TreeView1_AfterExpand(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterExpand
        skipnode = True ' KLUDGE

        If e.Node.ImageKey = "openfolder" Or e.Node.ImageKey = "closedfolder" Or e.Node.Name = String.Empty Then
            Me.TextBox3.Text = e.Node.Name & " (" & e.Node.Nodes.Count.ToString & " files)"
        Else
            Me.TextBox3.Text = e.Node.Text
        End If
    End Sub


    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick

        ' KLUDGE added to skip auto selection of first node
        If skipnode Then
            skipnode = False
            Exit Sub
        End If


        If e.Node.ImageKey = "openfolder" Or e.Node.ImageKey = "closed folder" Then Exit Sub
        If Allowed.Contains(e.Node.ImageKey) Then
            Dim response As DialogResult = MessageBox.Show("Select YES to stream this file over the Network", e.Node.Text, MessageBoxButtons.YesNo)
            If Not response = DialogResult.Yes Then Exit Sub
            Try
                Process.Start(e.Node.Name)
            Catch ex As Exception
                MessageBox.Show("Error opening file:   " & ex.Message)
            End Try
        End If
    End Sub

Regards Les, Livingston, Scotland

All replies (6)

Friday, April 22, 2016 2:10 AM ✅Answered | 1 vote

Hi

Well, I seem to have fixed the issue. I have no idea why this worked, as all I did was to delete the entire Double Click event handler and re-write it exactly but without the 'kludge'. (I used my OP of the original to be sure it was the same)

I can see absolutely no difference between the two versions yet the new version works perfectly.

I think I remember having a weird issue a long time ago that had a similar outcome where rewriting a sub cured a problem.

Maybe the issue is just me, that wouldn't surprise me either.

Sadly then, this thread doesn't really have a definitive answer, but I have marked up the contributions here.

Regards Les, Livingston, Scotland


Thursday, April 21, 2016 5:52 PM

I suppose it may be possible to use the TreeView.BeforeSelect Event to use e.cancel in some fashion to disallow selection of the first child node.

La vida loca


Thursday, April 21, 2016 6:08 PM

I suppose it may be possible to use the TreeView.BeforeSelect Event to use e.cancel in some fashion to disallow selection of the first child node.

La vida loca

Hi

Thanks for the reply. I had already tried messing about in that event handler, but anything I tried there was way longer than the Kludge I ended up with.

Regards Les, Livingston, Scotland


Thursday, April 21, 2016 10:12 PM | 1 vote

I can't replicate the issue with the below code. If I double click on a node that node highlights and remains highlighted even though it expands the child nodes of that node if there are any.

Treeview properties are default except Font.

Option Strict On

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TreeView1.BeginUpdate()
        TreeView1.Nodes.Add("Parent")
        TreeView1.Nodes(0).Nodes.Add("Child 1")
        TreeView1.Nodes(0).Nodes.Add("Child 2")
        TreeView1.Nodes(0).Nodes(1).Nodes.Add("Grandchild")
        TreeView1.Nodes(0).Nodes(1).Nodes(0).Nodes.Add("Great Grandchild")
        TreeView1.EndUpdate()
    End Sub

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        MessageBox.Show("Yes")
    End Sub

End Class

La vida loca


Thursday, April 21, 2016 10:31 PM

Hi

Yes, the same effect here, but what e.node is being passed to the double click handler?

When I double click on a node to expand it, quite rightly, the Node DoubleClick event is fired - *but*, in the sub arguments it has the node causing the 'fire' as the first node in  the expanded nodes list - even though it was the parent node that was actually double clicked.

This is causing my issue where double clicking on a 'folder' node to expand the node, the node double click code has acted as though I double clicked on the first node in the newly expanded list.

I suppose that since the 'kludge' I used does work, then I will have to stay with it. A pity, as I just can't figure out the logic behind an event being fired with what seems to me as the wrong arguments.

Regards Les, Livingston, Scotland


Thursday, April 21, 2016 10:47 PM | 1 vote

 I am not sure if i am understanding the problem you are having or if it is just that the problem is do to other things in the code.  When i use the NodeMouseDoubleClick even and double click a Node,  it does not automatically select the first child Node of the double clicked Node.  It also reports the correct Node that is double clicked in the TreeNodeMouseClickEventArgs as can be seen in the image below.

 

    Private Sub TreeView1_NodeMouseDoubleClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        Me.Text = e.Node.Text
    End Sub

 

 

If you say it can`t be done then i`ll try it