Results 1 to 7 of 7

Thread: treeview respond to single click [resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    21

    treeview respond to single click [resolved]

    I am working on a Windows Form application with a Treeview control. I would like the single click to fire up the Double click event. I am trying to respond to a single click on the Parent node description by expanding/collapsing the node. I would handle this in the AfterSelect event, but it is not fired if you single click an already selected node. How would I go about accomplishing this?
    Last edited by ingineu; Jun 5th, 2004 at 11:35 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Yes I noticed the same frustrating behaviour, if you change the node and then reselect it works fine. For the moment I've put in the I'll get around to it one day list.

    As a suggestion maybe the gotfocus event gets fired and you can check it's state and perhaps expand or collpase as you need. I don't know that is a solutuoin, just something to try.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I found a solution that worked great for me . It's like this : \


    in the TreeView BeforeSelect event , the code goes like this :

    PHP Code:
    if (e.Node.Parent==null
    {

    switch (
    e.Node.Index)
    {
    case (
    0); //this is the first parent node
    //do this 

    case (1);// second parent node .
    //do this 
    }


    This way , child node events won't fire .

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    21
    The BeforeSelect event does not fire if you click on an already selected Parent. Is there a way to, in the treeview Click event, fire the treeview DoubleClick event from code? I'm relatively new to VB.Net. I am converting from Access 2002, where the treeview control did exactly what I was after.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    21
    In the MouseDown event, is there a way I can tell whether the Treeview Label was clicked as opposed to the +/- box?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This always works fine with me . You can make use of param named "TreeViewCancelEventArgs". It gives you some info about everything on the node that's in action .
    VB Code:
    1. Private Sub TreeView1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
    2.         If e.Node.Parent Is Nothing Then
    3.             If e.Action.ByMouse = TreeViewAction.ByMouse AndAlso e.Action.Collapse = TreeViewAction.Collapse Then
    4.                 MsgBox("You pressed Parent node")
    5.             End If
    6.         End If
    7.     End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    21

    treeview respond to single click [resolved]

    Finally got it to work. (Pirate: the beforeselect event does not respond to clicking an already active parent, which is why it was necessary to use the MouseDown event)

    Private Sub tvwMenu_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwMenu.AfterSelect
    Dim strKey As String
    If (Not e.Node.Parent Is Nothing) Then
    strKey = e.Node.Parent.Index & e.Node.Index
    Else
    strKey = e.Node.Index
    End If
    Select Case strKey
    Case "00"
    ...
    Case "01"
    ...
    End Select
    End Sub

    Private Sub tvwMenu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwMenu.MouseDown
    m_Status = "New"
    Dim node As TreeNode
    node = tvwMenu.GetNodeAt(e.X, e.Y)
    If Not (node Is Nothing) And (node.Parent Is Nothing) Then
    If node.IsExpanded Then
    tvwMenu.GetNodeAt(e.X, e.Y).Collapse()
    m_Status = "Done"
    Else : tvwMenu.GetNodeAt(e.X, e.Y).Expand()
    m_Status = "Done"
    End If
    End If
    End Sub

    Private Sub tvwMenu_BeforeCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvwMenu.BeforeCollapse
    If m_Status = "Done" Then
    e.Cancel = True
    End If
    End Sub

    Private Sub tvwMenu_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvwMenu.BeforeExpand
    If m_Status = "Done" Then
    e.Cancel = True
    End If
    End Sub
    Last edited by ingineu; Jun 5th, 2004 at 11:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width