3

I am exploring Virtual Treeview in Delphi and ran a sample program where the editor is invoked by pressing F2 beginning the editing process uses the built-in editor in Virtualtreeview (No attached editing component). The text changed, but immediately changed back to the original when I clicked on a different node.

This led me to explore the source code in VirtualTrees.pas to study how the editing process works. Everything appears to boil down to the TBaseVirtualTree.doedit. I have examined each step but am uncertain what exactly operates the editing box positioned in the column.

procedure TBaseVirtualTree.DoEdit;

begin
  Application.CancelHint;
  StopTimer(ScrollTimer);
  StopTimer(EditTimer);
  DoStateChange([], [tsEditPending]);
  if Assigned(FFocusedNode) and not (vsDisabled in FFocusedNode.States) and
    not (toReadOnly in FOptions.FMiscOptions) and (FEditLink = nil) then
  begin
    FEditLink := DoCreateEditor(FFocusedNode, FEditColumn);
    if Assigned(FEditLink) then
    begin
      DoStateChange([tsEditing], [tsDrawSelecting, tsDrawSelPending, tsToggleFocusedSelection, tsOLEDragPending,
        tsOLEDragging, tsClearPending, tsDrawSelPending, tsScrollPending, tsScrolling, tsMouseCheckPending]);
      ScrollIntoView(FFocusedNode, toCenterScrollIntoView in FOptions.SelectionOptions,
        not (toDisableAutoscrollOnEdit in FOptions.AutoOptions));
      if FEditLink.PrepareEdit(Self, FFocusedNode, FEditColumn) then
      begin
        UpdateEditBounds;
        // Node needs repaint because the selection rectangle and static text must disappear.
        InvalidateNode(FFocusedNode);
        if not FEditLink.BeginEdit then
          DoStateChange([], [tsEditing]);
      end
      else
        DoStateChange([], [tsEditing]);
      if not (tsEditing in FStates) then
        FEditLink := nil;
    end;
  end;
end;

So my question is how is the actual keyboard input being placed in the node.text by VirtualTree and how is the result of the edit placed into the data record?

Ashlar
  • 636
  • 1
  • 10
  • 25

1 Answers1

6

You need to handle the OnNewText event eg:

procedure TForm1.VSTNewText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; Text: UnicodeString);
var
  data: TMyData;
begin
  data := TMyData(Sender.GetNodeData(Node)^);
  if Assigned(data) then
  begin
    if Column = 0 then
      data.Caption := Text
    else
      data.Value := Text;
  end;
end;

This event is called right after you edit the text in the editor.

The editor is implemented via the IVTEditLink interface. FEditLink.BeginEdit starts the process of the editing.

The build-in editor TStringEditLink implements IVTEditLink, and if you want to know how that works, You need to study the code.

If you need to use your own editor (eg a ComboBox like editor) you will need to implement IVTEditLink and return your EditLink in the OnCreateEditor event.

There are a few good examples of property editors in the Demo directory of the VST, that show how to implement your own editors.

kobik
  • 21,001
  • 4
  • 61
  • 121