poy
poy

Reputation: 10507

.net TreeView double click bug

So I've seen alot of talk about this on the blog-o-sphere... But I can't tell if it's a bug, or just bizzar behaviour that I don't understand...

Say you have a TreeView with CheckBoxes...

Double-click on a CheckBox on one of the Nodes, and then click on the box again... you'll notice that the state doesn't seem to change properly.

Is this a bug?? I'm always cautious about saying I found a "bug" in .NET...

Upvotes: 4

Views: 1268

Answers (2)

Sertan Pekel
Sertan Pekel

Reputation: 611

I exprerienced similar problem with treeview selection. I had managed Treeview.NodeMouseDoubleClick Event which each node runs with Mouse Double Click and executes different process. But when I double click on treeview (not over the node) code had crashed. I solved the problem when I realized treeview.SelectedNode is not the same one which Treeview.NodeMouseDoubleClick.TreeNodeMouseClickEventArgs e.Node parameter returns.

Thus, if you want to select a node with mouse selection on treeview, you need to click exactly on it.

Hope this helps.

Upvotes: 0

test
test

Reputation: 2639

Duplicating the problem shows that this is definitely a bug* (intentional asterisk). Because when you double-click an unchecked check box in a TreeView, then call code like this:

For Each node As TreeNode In Me.TreeView1.Nodes
    Console.WriteLine("{0}: {1}", node.Name, node.Checked)
Next

You will find that, while visually it is unchecked, the node thinks it is still checked. I can see a couple things happening:

  1. The AfterCheck event is called once during a checkbox double-click.
  2. Starting with an unchecked node's checkbox and double-clicking it you will find that the node thinks it is still checked (as mentioned above). This also explains why the next click does not change the checkbox to checked. It is because it thinks it is checked and is therefore setting the checked property to false, which isn't changing the UI. The next click after that sets it back to checked which is expected.
  3. It looks like the checkbox is actually taking focus somehow. If I double-click the treenode then go to click a button on the form, it takes two clicks. The first is to de-focus what I assume to be the checkbox, the next is to actually click the button.

My conclusion is that somehow, double-clicking is focusing the checkbox which is causing the second mouse click to not be sent to the treenode but it is sent to the checkbox, that is why the checkbox gets unchecked and the treenode is none-the-wiser.

* This is behavior that should not be occurring, how to classify it, I'll leave up to Microsoft.

Upvotes: 5

Related Questions