1

I'm trying to create a custom acting TreeView. When you click a node it should toggle as selected/unselected. Currently I can select a node once by clicking it, deselect the node by clicking it again, but I am unable to select the node again via clicking unless I select another node first. Any help would be greatly appreciated.

TreeNode lastNode;

private void treeViewMS1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if (lastNode == e.Node)
    {
        treeViewMS1.SelectedNode = null;
        lastNode = null;
    }
    else
    {
        if (lastNode == null)
        {
            treeViewMS1.SelectedNode = e.Node;
        }
        lastNode = e.Node;
    }
}
0

1 Answer 1

1

Try using the BeginInvoke procedure to delay the action until after the mouse click event is done processing. It's probably interfering:

TreeNode lastNode;
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
  this.BeginInvoke(new Action(() => {
    if (lastNode == e.Node) {
      treeView1.SelectedNode = null;
      lastNode = null;
    } else {
      if (lastNode == null) {
        treeView1.SelectedNode = e.Node;
      }
      lastNode = e.Node;
    }
  }));
}

If the Action method isn't available, you can use the MethodInvoker style:

this.BeginInvoke((MethodInvoker)delegate {
Sign up to request clarification or add additional context in comments.

7 Comments

Forgive me, how do I do that?
@DanGifford How do you do what? I posted the code.
Your code generates the following compilation error: Error 1 Using the generic type 'System.Action<T>' requires 1 type arguments. Thoughts?
@DanGifford What .Net version are you using? Updated the post with the MethodInvoker version.
I'm still getting errors with the MethodInvoker technique.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.