Back to Devexpress

TreeListOperation Class

windowsforms-devexpress-dot-xtratreelist-dot-nodes-dot-operations-702ac4ff.md

latest5.9 KB
Original Source

TreeListOperation Class

Serves as the base for classes specifying operations performed on nodes.

Namespace : DevExpress.XtraTreeList.Nodes.Operations

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
public abstract class TreeListOperation
vb
Public MustInherit Class TreeListOperation

Remarks

The Tree List control enables you to traverse through nodes without writing recursive code. This can be done by calling methods of the TreeListNodesIterator class that is returned by the TreeList.NodesIterator property. Methods of this class require a TreeListOperation descendant to be transmitted to them as a parameter. This parameter specifies the operation performed on each visited node. Moreover, it can define which nodes from the predefined set are to be visited.

The TreeListOperation class is abstract. You must derive a custom class from it that will represent the desired operation. The examples of operations may be: calculating the number of nodes, calculating the sum of node values from the specified field, expanding each visited node, printing visited nodes.

The logic of the operation represented by a TreeListOperation descendant must be implemented in the TreeListOperation.Execute method. This method is automatically called for each visited node. The processed node is transmitted to it as the parameter. Thus, you can perform a specific operation on the current node (for instance, collapse it) or obtain the value of the predefined field to perform desired calculations.

The most simple example of a TreeListOperation descendant is an operation that calculates the number of visited nodes. This is used by the Tree List control internally to return the TreeList.AllNodesCount property value. In such a case, the TreeListOperation.Execute method, must simply increment an internal counter.

You can also override the TreeListOperation.FinalizeOperation method when performing calculations over nodes. This method is called after all nodes have been processed. Thus you can analyse the result and modify it, if needed.

Example

The following sample code shows you how to create a custom operation class. It is used to calculate the maximum length of cell values within the specified column. The created TreeListMaxLengthOperation class is derived from the TreeListOperation class for this purpose.

The TreeListNodesIterator.DoOperation method is used to perform the operation. The result of calculations is written into the result variable.

csharp
public class TreeListMaxLengthOperation : TreeListOperation {
   private string fieldName;
   private int maxLength;

   public TreeListMaxLengthOperation(string fieldName) {
      this.fieldName = fieldName;
      maxLength = 0;
   }
   public override void Execute(TreeListNode node) {
      string nodeText = node[fieldName].ToString();
      if (nodeText.Length > maxLength) maxLength = nodeText.Length;
   }
   public int MaxLength {
      get { return maxLength; }
   }
}

// ...
// performing the declared operation and storing its result
TreeListMaxLengthOperation operation = new TreeListMaxLengthOperation("Department");
treeList1.NodesIterator.DoOperation(operation);
int result = operation.MaxLength;
vb
Public Class TreeListMaxLengthOperation
   Inherits TreeListOperation

   Private _fieldName As String
   Private _maxLength As Integer

   Public Sub New(ByVal FieldName As String)
      Me._fieldName = FieldName
      _maxLength = 0
   End Sub

   Public Overrides Sub Execute(ByVal Node As TreeListNode)
      Dim NodeText As String = Node(_fieldName).ToString()
      If NodeText.Length > _maxLength Then _maxLength = NodeText.Length
   End Sub

   Public ReadOnly Property MaxLength() As Integer
      Get
         Return _maxLength
      End Get
   End Property
End Class

' ...
' performing the declared operation and storing its result
Dim Operation As New TreeListMaxLengthOperation("Department")
TreeList1.NodesIterator.DoOperation(Operation)
Dim Result As Integer = Operation.MaxLength

Inheritance

Object TreeListOperation TreeListVisibleNodeOperation

See Also

TreeListOperation Members

NodesIterator

TreeListVisibleNodeOperation

Tree Traversal

DevExpress.XtraTreeList.Nodes.Operations Namespace