Hi, Is there any way I can disable expanding TreeNode after doubleclick??
Thanks
Hi, Is there any way I can disable expanding TreeNode after doubleclick??
Thanks
Handles the event BeforeExpand and set the property Cancel of event args object to true.
private void OnBeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
}
The TreeViewCancelEventArgs contains also the affected node and the action that caused the event so you can implement a more sophisticated logic to enable or disable the expand behavior than my simple example.
There is no simple way to achieve this, as far as I know. One thought would be to have a bool
variable set to true
on the DoubleClick event, and use the e.Cancel
property of the BeforeExpand
event to prevent the node from expanding. However, those two events are fired in the opposite order, so that is not a solution. I don't have another solution from the top of my head; will update if I come up with one.
Update
I have played around with this a bit, and I have found one way that works reasonably well. As I have mentioned the problem is that BeforeExpand
happens before DoubleClick
, so we can't set any state in DoubleClick
to use in BeforeExpand
.
We can, however, detect (potential) double clicks in another way: by measuring the time between MouseDown
events. If we get two MouseDown
events within the time period that defines a double click (as stated in SystemInformation.DoubleClickTime
), it should be a double click, right? And the MouseDown
event is raised before BeforeExpand
. So, the following code works rather well:
private bool _preventExpand = false;
private DateTime _lastMouseDown = DateTime.Now;
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = _preventExpand;
_preventExpand = false;
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
int delta = (int)DateTime.Now.Subtract(_lastMouseDown).TotalMilliseconds;
_preventExpand = (delta < SystemInformation.DoubleClickTime);
_lastMouseDown = DateTime.Now;
}
I say "rather well" because I feel that it prevents the node from expanding in some cases when it should not (for instance if you within the double click time click first on the node text and then on the plus sign). That might be possible to solve in some way, or perhaps you can live with that.
Yes. You can do this.
Somewhere by itself in the code write this:
bool CanExpand = true;
... and inside the doubleclicked handler write this:
CanExpand = false;
.......and inside the um.. the BeforeExpand event, type this:
if( CanExpand )
Application.DoEvents();
else e.Cancel = true;
I hope this has helped.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool CanExpand = true;
private void treeView1_DoubleClick(object sender, EventArgs e)
{
CanExpand = false;
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (CanExpand)
Application.DoEvents();
else
e.Cancel = true;
}
}
}