Skip to main content

Focus and Navigation

  • 3 minutes to read

You can move focus between nodes using the mouse or keyboard. It’s also possible to focus nodes in code. See the following topics, to learn how to do this.

Focus Movement

The currently focused node is specified by the TreeList.FocusedNode property. You can read its value to access the focused node. Assigning a value to this property results in moving focus to the specified node.

Example

The following example shows how to move focus to a specific node. To do this, the requried node is assigned to the TreeList.FocusedNode property.

// Move focus to the first root node.
treeList1.FocusedNode = treeList1.Nodes[0];

This is not the only way to move focus between nodes. Other methods for moving focus can be divided into two groups. The first group of methods enables you to move focus through nodes that are not hidden within collapsed groups. These are the TreeList.MoveNextVisible, TreeList.MovePrevVisible and TreeList.MoveLastVisible methods. They allow you to move focus to the next, previous and last visible nodes respectively.

The second group of methods allows you to navigate through all nodes regardless of whether they are hidden within collapsed groups or not. These are the TreeList.MoveNext, TreeList.MovePrev and TreeList.MoveLast methods. These methods expand all parent nodes of the node being focused.

Use the TreeList.MoveFirst method to focus the first node within the XtraTreeList. This method doesn’t have a pair since the first node is always a root node, and thus, cannot be hidden.

Let’s consider an example of using methods from the groups mentioned above. The first screenshot displays the initially focused node.

Focus-Beginning

The following table visually demonstrates the difference between the two groups of methods.

Calling the MovePrevVisible method

Calling the MovePrev method

The MovePrevVisible method focuses the “Sales and Marketing” node - the previous visible node.

Focus-PrevVisible

The MovePrev method expands the previous visible “Sales and Marketing” node, and focuses its last child - “Marketing”.

Focus-Prev

Let’s see one more example of using navigation methods from different groups. The “Consumer Electronics Div.” node is initially focused.

Focus-Next-Beginning

The table below illustrates the difference between the TreeList.MoveNext and TreeList.MoveNextVisible methods.

Calling the MoveNextVisible method

Calling the MoveNext method

The MoveNextVisible method moves focus to its next visible node - “Software Products Div.”.

Focus-NextVisible

The MoveNext method moves focus to the first child of the “Consumer Electronics Div.” node.

Focus-Next

Respond to Focus Movement

You can respond to moving focus between nodes by handling the TreeList.BeforeFocusNode, TreeList.AfterFocusNode and TreeList.FocusedNodeChanged events.

The TreeList.BeforeFocusNode event fires before a node is focused. It allows you to identify the previously focused node and the node being focused. You can also cancel the current focus operation by setting the BeforeFocusNodeEventArgs.CanFocus event’s parameter to false.

The TreeList.AfterFocusNode and TreeList.FocusedNodeChanged events fire after a node has been focused. The only difference is that the second event provides the previously focused node as its parameter.

Example

The following example prohibits focusing the first node within the control, by handling the TreeList.BeforeFocusNode event.

private void treeList1_BeforeFocusNode(object sender, 
DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e) {
   if (e.Node.Id == (sender as TreeList).Nodes[0].Id) 
      e.CanFocus = false;
}

Member Tables

End-User Capabilities