Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

VB.NET code to prevent Expand() and Collapse() from firing when TreeView is doubleclicked.

 

Add this class to your project:

   'DoubleClick-expand restriction hack
   Public Class cTreeViewDblClickHack
       Private WithEvents m_tv As TreeView
       Public Sub New(ByVal tv As TreeView)
           m_tv = tv
       End Sub
       Private m_FirstMouseDownTime As Long
       Private blnDoubleClick As Boolean = False
       Private Sub tv_BeforeExpand(ByVal sender As Object, _
                                   ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _
                                   Handles m_tv.BeforeExpand
           If blnDoubleClick AndAlso e.Action = TreeViewAction.Expand Then
               e.Cancel = True
           Else
               m_FirstMouseDownTime = 0
           End If
       End Sub

       Private Sub tv_BeforeCollapse(ByVal sender As Object, _
                                     ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _
                                     Handles m_tv.BeforeCollapse
           If blnDoubleClick AndAlso e.Action = TreeViewAction.Collapse Then
               e.Cancel = True
           Else
               m_FirstMouseDownTime = 0
           End If
       End Sub

       Private Sub tv_MouseDown(ByVal sender As Object, _
                                ByVal e As System.Windows.Forms.MouseEventArgs) _
                                Handles m_tv.MouseDown
           If Now.Ticks - m_FirstMouseDownTime <= (SystemInformation.DoubleClickTime * TimeSpan.TicksPerMillisecond) Then
               blnDoubleClick = True
           Else
               blnDoubleClick = False
           End If
           m_FirstMouseDownTime = Now.Ticks
       End Sub
   End Class

 

And use it in your Form like this:

   Private m_TreeViewDblClickHack As cTreeViewDblClickHack
   Public Sub New()
       MyBase.New()
       'This call is required by the Windows Form Designer.
       InitializeComponent()

       'Add any initialization after the InitializeComponent() call
       m_TreeViewDblClickHack = New cTreeViewDblClickHack(Me.tv)
   End Sub

 

TreeView is an annoying piece of **** :(

 

I got the idea from here:

http://www.windowsforms.net/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=15689

Edited by keitsi
BS - Beer Specialist
  • 3 years later...
Posted

Here is the C# equiv:

 

public class TreeDblClickPrevent

{

private TreeView m_tv;

public TreeDblClickPrevent(TreeView tv)

{

m_tv = tv;

tv.BeforeExpand +=new TreeViewCancelEventHandler(tv_BeforeExpand);

tv.MouseDown +=new MouseEventHandler(tv_MouseDown);

tv.BeforeCollapse += new TreeViewCancelEventHandler(tv_BeforeCollapse);

}

private long m_FirstMouseDownTime;

private bool blnDoubleClick = false;

 

private void tv_BeforeExpand(object sender, TreeViewCancelEventArgs e){ //handles m_tv.BeforeExpand

if(blnDoubleClick == true && e.Action == TreeViewAction.Expand)

e.Cancel = true;

else

m_FirstMouseDownTime = 0;

}

 

private void tv_BeforeCollapse(object sender, TreeViewCancelEventArgs e){ //Handles m_tv.BeforeCollapse

if(blnDoubleClick == true && e.Action == TreeViewAction.Collapse)

e.Cancel = true;

else

m_FirstMouseDownTime = 0;

}

 

private void tv_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e){// Handles m_tv.MouseDown

if(System.DateTime.Now.Ticks - m_FirstMouseDownTime <= (SystemInformation.DoubleClickTime * TimeSpan.TicksPerMillisecond))

blnDoubleClick = true;

else

blnDoubleClick = false;

m_FirstMouseDownTime = System.DateTime.Now.Ticks;

}

}

 

 

 

Then in your form just pass the treeview in question to the attach the handlers:

TreeDblClickPrevent treep = new TreeDblClickPrevent(tvMain);

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...