locked
How to prevent Collapse when Double Click TreeNode ? RRS feed

  • Question

  • Dear all,

    When user double click a treenode, a form will be open for user modifying

    but its childrent Node is collapsed automatically.

    Does TreeView have method to prevent this happen ?

    Thanks so much.

    Wednesday, January 9, 2008 9:01 AM

Answers

  • u can use the

    BeforeCollapse

    in conjunction with NodeMouseDoubleClick

     

    so in NodeMouseDoubleClick

    {

       //set a flag

       clicked = true;

    }

     

    then in BeforeCollapse

    {

      if(Clicked)

    {

         e.Cancel = true;

        Clicked = false;

    }

     

    }

    Wednesday, January 9, 2008 10:46 AM

All replies

  • this might help

    in

    NodeMouseDoubleClick

     

    do

     

    e.Node.Expand();

    //do other things

    Wednesday, January 9, 2008 9:37 AM
  • But user can see TreeNode is collaped before expand again.

    Is there any way to do like Window Explorer. TreeNode just collapse when user click this symbol (+) ?


    Wednesday, January 9, 2008 9:55 AM
  • u can use the

    BeforeCollapse

    in conjunction with NodeMouseDoubleClick

     

    so in NodeMouseDoubleClick

    {

       //set a flag

       clicked = true;

    }

     

    then in BeforeCollapse

    {

      if(Clicked)

    {

         e.Cancel = true;

        Clicked = false;

    }

     

    }

    Wednesday, January 9, 2008 10:46 AM
  • Thanks for your help.



    Thursday, January 10, 2008 2:31 AM
  • This doesn't work I'm afraid because the BeforeCollapse/Expand events fire before the DoubleClick and NodeMouseDoubleClick events as do the AfterCollapse/Expand events.

     

    I'd love to know what the solution to this is though, I can't think of one without getting very down and dirty with the TreeView and I don't know how to do that.

     

     

    Thursday, March 27, 2008 1:35 PM
  • Hello,

    I had the same Problem and solved it by deriving from TreeView and blocking the sending of the double click message to the treeview control. The Doubleclick Event is still triggered, so you can display your form.
     
    class MyTreeView : TreeView  
       {  
          private const int WM_LBUTTONDBLCLK = 0x203;  
     
          protected override void  DefWndProc(ref Message m)   
          {  
             if (m.Msg == WM_LBUTTONDBLCLK)  
                return;  
     
             base.DefWndProc(ref m);  
          }  
       } 

    Sinverely,
    Menne
    • Proposed as answer by Douglas Hauck Monday, December 7, 2009 8:58 PM
    Friday, February 20, 2009 12:50 PM
  • That's a winner, alright.  Now why didn't I think of that?
    Monday, December 7, 2009 8:59 PM
  • 		private const int WM_ERASEBKGND = 0x0014;
    		private const int WM_LBUTTONDBLCLK = 515;
    		protected override void DefWndProc(ref Message m)
    		{
    			if (m.Msg == WM_ERASEBKGND)
    			{
    				m.Result = IntPtr.Zero;
    				return;
    			}
    			else if (m.Msg == WM_LBUTTONDBLCLK)
    			{
    				int LParam = m.LParam.ToInt32();
    				int x = LParam & 0xffff;
    				int y = LParam >> 16;
    				var ht = this.HitTest(x, y);
    				if (ht.Location != TreeViewHitTestLocations.PlusMinus)
    					return;
    			}
    			base.DefWndProc(ref m);
    		}
    Wednesday, March 31, 2010 6:49 PM