Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to avoid TreeNode check from happening on a double click event

So I have a TreeView in a C# windows form app. What I need is for some nodes to be "locked" so that they cannot be checked (or unchecked), based on a parameter.

What I am doing now is this:

private void tv_local_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
    TNode node = (TNode)e.Node;
    //if a part node, cancel the action.
    if (node.Type == "Part") {
        e.Cancel = true;     
    }
    //if a locked node, cancel the action
    if (node.Locked == true) {
        e.Cancel = true;
    }
}

This code works great on a single click of the checkbox, but if the user double clicks on a checkbox, it still checks/unchecks.

I have tried playing with the nodeMouseDoubleClick event, but that doesnt really help, since I cannot cancel the event...

Is there any ideas out there how to cancel a double click event on a node?... or anything else? Thanks

like image 546
Toadums Avatar asked May 25 '11 20:05

Toadums


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

This is a bug in the TreeView I think (http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9d717ce0-ec6b-4758-a357-6bb55591f956/). You need to subclass the tree view and disable the double-click message in order to fix it. Like this:

public class NoClickTree : TreeView
    {
        protected override void WndProc(ref Message m)
        {
            // Suppress WM_LBUTTONDBLCLK
            if (m.Msg == 0x203) { m.Result = IntPtr.Zero; }
            else base.WndProc(ref m);
        }              
    };

Of course if you do this you'll no longer be able to use the double-click metaphor in the tree-view for other things (such as double click a node to launch a property page, or something).

like image 84
Robinson Avatar answered Sep 19 '22 23:09

Robinson


If you want your double click to actually toggle the check box then try:

protected override void WndProc(ref Message m)
{
  // Filter WM_LBUTTONDBLCLK when we're showing check boxes
  if (m.Msg == 0x203 && CheckBoxes)
  {
    // See if we're over the checkbox. If so then we'll handle the toggling of it ourselves.
    int x = m.LParam.ToInt32() & 0xffff;
    int y = (m.LParam.ToInt32() >> 16) & 0xffff;
    TreeViewHitTestInfo hitTestInfo = HitTest(x, y);

    if (hitTestInfo.Node != null && hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
    {
      OnBeforeCheck(new TreeViewCancelEventArgs(hitTestInfo.Node, false, TreeViewAction.ByMouse));
      hitTestInfo.Node.Checked = !hitTestInfo.Node.Checked;
      OnAfterCheck(new TreeViewEventArgs(hitTestInfo.Node, TreeViewAction.ByMouse));
      m.Result = IntPtr.Zero;
      return;
    }
  }

  base.WndProc(ref m);
}
like image 24
PhilP Avatar answered Sep 19 '22 23:09

PhilP