Results 1 to 5 of 5

Thread: [2005] [RESOLVED] Prevent TreeNode from Collapsing when DoubleClicked but...

  1. #1

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Resolved [2005] [RESOLVED] Prevent TreeNode from Collapsing when DoubleClicked but...

    Hi,

    I have got a standard Windows Forms Treeview which contains some nodes.

    I am using NodeMouseDoubleClick event to get the node and then get the ID of the node and show a form containing data that belongs to the ID.

    What I am stuck at is when I double click any node, it Collapses or Expands depending on it's previous state.

    I want to Prevent TreeNode from Collapsing or Expanding when DoubleClicked but allow when user clicks that tiny "+" or "-" button in front of the node.

    Is this possible? Is so then could someone guide me to the solution please?

    Cheers
    Last edited by wrack; Feb 16th, 2009 at 09:37 PM. Reason: [RESOLVED]

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...

    The treeview exposes some events for when a node is expanded/collapsed.

    Look at the BeforeCollapse and BeforeExpand events

    Using the event args in those events you can decide if you want to set e.cancel to true to prevent it from happening.

    The solution may include the need to set a boolean variable in your double click code, so you know if you should set cancel to true in these events.

  3. #3

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...

    Thanks Kleinma, I tried those but the trouble is when I double click a node, the before collapse event fires before that and thus expanding or collapsing the node and then the double click event happening!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...

    It's not ideal but you could simply undo the operation on a double-click:
    vb.net Code:
    1. Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, _
    2.                                            ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    3.     If e.Node.IsExpanded Then
    4.         e.Node.Collapse()
    5.     Else
    6.         e.Node.Expand()
    7.     End If
    8. End Sub
    Other than that I think you'd have to get into trapping Windows messages to know when the node was expanding or collapsing due to a double-click.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Resolved Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...

    Thanks Jim,

    I knew it would come down to this solution, I had it already but I just didn't wanted to go to the WM_Messages path!

    I have put this code for now, in C# actually.

    Code:
            private void tvwChildren_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                string sEntityID = String.Empty;
    
                if (e.Node != null)
                {
                    sEntityID = e.Node.Name.ToString();
                }
    
                try
                {
                    tvwChildren.SuspendLayout();
    
                    if (e.Node.IsExpanded)
                    {
                        e.Node.Collapse();
                    }
                    else
                    {
                        e.Node.Expand();
                    }
                }
                finally
                {
                    tvwChildren.ResumeLayout(true);
                }
            }   // tvwChildren_NodeMouseDoubleClick
    Thanks for the heads up.

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