Interaction
Mouse and keyboard navigation
TTMSFNCTreeView responds to mouse clicks and keyboard navigation out of the box:
- Click a node to select it.
- Up/Down, Home, End, Page Up/Down move selection.
- Left/Right keys collapse and expand nodes.
- Clicking the expand/collapse icon has the same effect.
Multi-select
TreeView1.Interaction.MultiSelect := True;
// Use Ctrl+click or Shift+click to select multiple nodes
Retrieve selected nodes:
var
I: Integer;
begin
for I := 0 to TreeView1.SelectedNodeCount - 1 do
ShowMessage(TreeView1.SelectedNodes[I].Text[0]);
end;
Select all:
TreeView1.SelectAllNodes; // collection-based
TreeView1.SelectAllVirtualNodes; // virtual
Mouse wheel sensitivity
Control how much the tree scrolls per wheel notch:
TreeView1.Interaction.MouseWheelDelta := 2; // scroll 2 node heights per notch
Inplace editing
Enable editing per column via the EditorType property:
TreeView1.Columns[0].EditorType := tcetEdit; // standard TEdit
TreeView1.Columns[1].EditorType := tcetComboBox; // TComboBox with fixed list
TreeView1.Columns[1].EditorItems.Add('Option A');
TreeView1.Columns[1].EditorItems.Add('Option B');
Start editing with a click on the text or by pressing F2.
Control text when editing starts via OnGetNodeText:
procedure TForm1.TreeView1GetNodeText(Sender: TObject;
ANode: TTMSFNCTreeViewVirtualNode; AColumn: Integer;
AMode: TTMSFNCTreeViewNodeTextMode; var AText: string);
begin
if AMode = tntmEditing then
AText := ''; // start with a blank editor
end;
Validate before committing:
procedure TForm1.TreeView1BeforeUpdateNode(Sender: TObject;
ANode: TTMSFNCTreeViewVirtualNode; AColumn: Integer;
var AText: string; var ACanUpdate: Boolean);
begin
if AText = '' then
ACanUpdate := False; // reject empty
end;
Custom editor
Supply any TControl subclass as the inplace editor:
TreeView1.Columns[1].CustomEditor := True;
procedure TForm1.TreeView1GetInplaceEditor(Sender: TObject;
ANode: TTMSFNCTreeViewVirtualNode; AColumn: Integer;
var ATransparent: Boolean;
var AInplaceEditorClass: TTMSFNCTreeViewInplaceEditorClass);
begin
AInplaceEditorClass := TTrackBar;
end;
procedure TForm1.TreeView1BeforeUpdateNode(Sender: TObject;
ANode: TTMSFNCTreeViewVirtualNode; AColumn: Integer;
var AText: string; var ACanUpdate: Boolean);
begin
AText := FloatToStr((TreeView1.GetInplaceEditor as TTrackBar).Value);
end;
Lookup
When Interaction.Lookup.Enabled is True, typing alphanumeric characters navigates to the node whose text starts with the typed string:
TreeView1.Interaction.Lookup.Enabled := True;
Filtering
Filtering hides nodes that don't match a condition while keeping their ancestors visible so the tree stays navigable. Enable column-header filter dropdowns or apply a programmatic filter via the Filter collection.
// Enable or disable filtering on a per-column basis
TreeView1.Columns[0].Filtering.Enabled := True;
TreeView1.Columns[1].Filtering.Enabled := False;
Dropdown filter
Adds a filter button to the column header. Clicking it shows a dropdown list of unique values; selecting one filters the tree.
TreeView1.Columns[0].Filtering.Enabled := True;
Programmatic filter
procedure TForm1.ApplyFilter;
var
f: TTMSFNCTreeViewFilterData;
begin
TreeView1.Filter.Clear;
f := TreeView1.Filter.Add;
f.Column := 0;
f.Condition := '*A*'; // wildcard match on column 0
f := TreeView1.Filter.Add;
f.Column := 1;
f.Condition := '>= 2010'; // range match on column 1
TreeView1.ApplyFilter;
end;
Note
When a child node matches the filter condition, its entire parent chain is also shown so the tree structure remains navigable.
Remove all active filters:
TreeView1.RemoveFilters;
Sorting
Enable click-to-sort on a column:
TreeView1.Columns[0].Sorting := tcsRecursive; // also sorts child nodes
Sort programmatically:
TreeView1.Sort(0, True, False, nsmDescending);
// parameters: column index, recursive, case-sensitive, sort direction
Clipboard
TreeView1.Interaction.ClipboardMode := tcmFull;
// tcmTextOnly — copies text only
// tcmFull — copies all node attributes (text, icons, check states)
Cut/Copy/Paste are handled via standard keyboard shortcuts (Ctrl+X, Ctrl+C, Ctrl+V). When pasting, the focused node becomes the parent of the pasted nodes; if no node is focused, pasted nodes are added at root level.
Reordering and drag-and-drop
Reordering within the same level
TreeView1.Interaction.Reorder := True;
// Click an already-selected node and drag to a new position
Drag-and-drop
TreeView1.Interaction.DragDropMode := tdmMove; // or tdmCopy
// Drag-and-drop takes precedence over reorder
// Supports moving nodes between two different TTMSFNCTreeView instances
Drag-and-drop events: OnBeforeDropNode, OnAfterDropNode, OnAfterReorderNode.
Combined example: filtering + sorting + editing
var ACanUpdate: Boolean);
begin
// Validate or transform the text before it is committed
if (AColumn = 0) and (AText = '') then
ACanUpdate := False; // reject empty text
end;
// Clipboard
procedure TForm1.SetupClipboard;
begin
Interaction property reference
| Property | Description |
|---|---|
ClipboardMode |
tcmNone, tcmTextOnly, tcmFull |
ColumnAutoSizeOnDblClick |
Autosize a column by double-clicking the header splitter |
ColumnSizing |
Allow the user to resize columns by dragging |
DragDropMode |
tdmNone, tdmMove, tdmCopy |
ExtendedEditable |
Allow editing extended (section-header) nodes |
ExtendedSelectable |
Allow selecting extended nodes |
KeyboardEdit |
Start editing with F2 or direct typing |
Lookup.Enabled |
Type-to-navigate |
MouseEditMode |
When mouse click starts editing |
MouseWheelDelta |
Scroll sensitivity (node heights per wheel notch) |
MultiSelect |
Allow Ctrl/Shift multi-selection |
ReadOnly |
Disable all editing |
Reorder |
Enable drag-reorder within the same level |
TouchScrolling |
Enable inertial touch scroll |
Related API
TTMSFNCTreeView—Sort,Filter,ApplyFilter,RemoveFilters,SelectAllNodesTTMSFNCTreeViewData—TTMSFNCTreeViewFilterData
See also
- Columns and groups — per-column filtering and sorting configuration
- Nodes — node types and structure