tags:

views:

246

answers:

3

Hi, Is there any way I can disable expanding TreeNode after doubleclick??

Thanks

A: 

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.

Francis B.
Hi,Unfortunately I don`t see action (like doubleclick or simple expand) in TreViewCancelEventArgs
arek
+1  A: 

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.

Fredrik Mörk
Actually, there is a very simple way of achieving this.
baeltazor
@baeltazor: I tried out the solution you suggest in your answer prior to posting my answer. See my comments under your answer.
Fredrik Mörk
It`s quite interesting solution but it works :)Thanks :)
arek
+1  A: 

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.


I just tested this, and it works flawlessly. :-) Below is the whole code that I used to test it. So you can see how it works:

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;
        }
    }
}
baeltazor
I just tried this out and it shows a *very* inconsistent behavior (it allows the first expand on doubleclick, since `BeforeExpand` is raised before `DoubleClick`, then it blocks the next expand attempt, regardless of how it is triggered). Also, your code should probably set `CanExpand = true` last in the `BeforeExpand` event handler, or it will block all future expand attempts.
Fredrik Mörk