Define Keyboard Mappings
When you alter the default behavior of a user-interface control, you must consider not only how users access new or modified features with the mouse, but also how you can let them use the keyboard. By default, the TreeView control supports these keys:
- Pressing the up and down (or left and right) arrow keys move the selected item up or down to the next visible control, whether on the same or a different level.
- Pressing the Enter key collapses or expands a node, depending on its current state. When you're on an expanded node and you press the right arrow key, the selection moves to the next lower visible node. If needed, the control expands collapsed nodes, all the way to a terminal node. If you select a collapsed node and then press the left arrow key, the selection moves to its parent node. Keep pressing the left arrow key on collapsed nodes to move all the way to a root node.
The project's code doesn't interfere with these basic navigational keystrokes. But the default keystroke set doesn't include the ones necessary to let users edit the control's contents; therefore, the project recognizes several additional keystrokes required to perform basic editing operations, as follows:
Implement Keystroke Handlers
- Inserting new nodes. To add a new node under the selected one, press the Ins key. To add a new root node, press Control+Insert. After you add a new node, the code places the control in edit mode automatically, letting you edit the new node's label. To end any edit operation, press the Enter key. After you finish editing the new node's label, the program selects the parent node, so you can add a new node under the same parent by pressing the Insert key again.
- Editing node labels. To edit a node's label, press the Space bar, or the F2 key (the Enter key might be a better choice, but by default, Enter expands or collapses a node, depending on its current state). The control places the selected node's label in edit mode. You end the edit operation by pressing Enter.
- Deleting nodes. To delete a node, select it and press the Delete key.
- Dragging nodes. You can change the hierarchy of the nodes by dragging a node and dropping it on another node. The control moves the original node to its new position as a child of the target node (the node on which the drop operation took place). If you want to copy a node under a new parent, hold the Shift key as you drag and drop it. If the selected node contains child nodes of its own, the entire hierarchy under the selected node will be copied or moved.
Now that you've defined the keystrokes that control the new features, you must implement them in code. In the sample, the TreeView control's KeyDown
event handler processes all editing operations that involve keystrokes. It consists of a Select Case
block with a separate Case
command for each keystroke you're interested in.
When a user presses the space bar, the code places the selected node in edit mode by calling its StartLabelEdit
method. When the user presses the Delete
key, the code removes the selected node by calling the TreeView.Nodes
method. When the user presses the Insert
key, the code adds a new node as a child of the selected node and places it in edit mode with the following statements:
Set insNode = TreeView1.Nodes.Add _
insNode.Text = ""
To terminate edit mode, the user must press Enter again. If the user presses Control+Insert
, the code adds a new root node and places it in edit mode:
If Shift And vbCtrlMask Then
Set insNode = TreeView1.Nodes.Add()
insNode.Selected = True
Each node must have a keya string identifier. The sample project creates a key for newly added or edited nodes when the edit operation ends by generating a random number between 1 and 10000000, prefixed with the "K" character (the key can't be a numeric value). Because the Rnd()
function doesn't generate unique random values, the code generates the key within a loop, testing each time by attempting to set the Key
property. If the generated key is already in use, VB raises an error, in which case the code simply repeats the loop to choose a different number.
Dim Repeat As Boolean
Repeat = True
On Error Resume Next
TreeView1.SelectedItem.Key = "K" & 1 + _
Int(Rnd() * 10000000)
If Err.Number = 0 Then Repeat = False
The editing operations themselves are straightforward and completely keyboard-driven.