Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeView double-click behaviour in .NET / C#

I have a regular .NET Windows Forms treeview control. The nodes are setup like this:

Group

---child

---child

If I double-click a collapsed Group node, it expands (as you'd expect) and the NodeMouseDoubleClick event is fired off, where my code does something if the selected node is NOT a group node.

The problem arises when the Group is located near the bottom of the treeview, so that when I double-click the Group node it would require the treeview to expand vertically to fit the child nodes into view. In such cases, if I double-click the Group node, by the time it expands and adjusts the treeview, my mouse cursor is over a child node (it had to push everything up), and that causes the NodeMouseDoubleClick to think the child node is selected, which causes very odd behaviour.

How can I get around this? Should I not be using NodeMouseDoubleClick or..?

I see it was also explained in the feedback report Problem with TreeView DoubleClick event after expanding/collapsing caused change of scroll.

like image 596
Ivan Avatar asked Jan 23 '09 14:01

Ivan


People also ask

What is TreeView in C#?

The TreeView control contains a hierarchy of TreeViewItem controls. It provides a way to display information in a hierarchical structure by using collapsible nodes . The top level in a tree view are root nodes that can be expanded or collapsed if the nodes have child nodes.

How do I know if TreeView node is selected?

Solution 1Use TreeView. SelectedNode[^] property, which get or set selected node for currently selected Treeview. If no TreeNode is currently selected, the SelectedNode property is a null reference (Nothing in Visual Basic).

What is TreeView node?

A tree view is a component to navigate hierarchical lists. It is made up of one or more top level nodes. A node may have children or it may be an end node. Nodes with children can be expanded or collapsed - when expanded its child nodes are visible.

What is the mousedoubleclick event in TreeView?

This means that when the user double-clicks on an item, you can execute code based on that node. Tip: To add the MouseDoubleClick event handler, right-click the TreeView in the designer and select Properties. Then, select "MouseDoubleClick" and click twice on that entry.

How to create a TreeView control in Visual Studio 2017?

To do this, open the Toolbox panel by clicking on the View and then Toolbox menu item in Visual Studio. Double-click on the TreeView item. Now, double-click on the Form1 window in the designer so you can create the Form1_Load event. In this event handler, we will insert code to build the nodes in the TreeView control.

How to build nodes in the TreeView control?

Double-click on the TreeView item. Now, double-click on the Form1 window in the designer so you can create the Form1_Load event. In this event handler, we will insert code to build the nodes in the TreeView control.

Does TreeView expand/collapse when you double click on a node?

Normally when using treeview I would expect that when the user double-clicking on a node that has children the tree will expand/collapse. However, Say we have a tree in a selection dialog.


2 Answers

The NodeDoubleClick is fine, but instead of using the e.Node, use this.treeView1.SelectedNode.

like image 180
BFree Avatar answered Oct 02 '22 19:10

BFree


Double-clicking a TreeNode is a mouse gesture that is already "used" by the TreeView to collapse/expand nodes Microsoft doesn't push the UI standards as much as Apple does, and on some level it is disappointing that Microsoft has exposed NodeDoubleClick, because they are encouraging you to amend the TreeView with your own custom behavior. This can be misleading to end users, who expect common behavior from common controls.

From Designing the User Interface by Ben Shneiderman, the first of Eight Golden Rules of Interface Design:

  1. Strive for consistency.

Consistent sequences of actions should be required in similar situations; identical terminology should be used in prompts, menus, and help screens; and consistent commands should be employed throughout.

Long story short, maybe you should not be using NodeMouseDoubleClick.

like image 38
tonyz Avatar answered Oct 02 '22 21:10

tonyz