我正在Delphi中探索Virtual Treeview并运行一个示例程序,其中按F2调用编辑器,开始编辑过程使用Virtualtreeview中的内置编辑器(无附加编辑组件)。文本已更改,但在我单击其他节点时立即更改回原始文本。
这促使我探索VirtualTrees.pas中的源代码,以研究编辑过程的工作原理。一切似乎都归结为TBaseVirtualTree.doedit
。我已经检查过每一步但不确定究竟是什么操作了列中的编辑框。
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;
所以我的问题是如何将实际的键盘输入放在virtualTree的node.text中,编辑的结果如何放入数据记录中?
答案 0 :(得分:4)
您需要处理OnNewText
事件,例如:
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;
在编辑器中编辑文本后立即调用此事件。
编辑器通过IVTEditLink
界面实现。 FEditLink.BeginEdit
开始编辑过程。
内置编辑器TStringEditLink
实现IVTEditLink
,如果您想知道它是如何工作的,您需要学习代码。
如果您需要使用自己的编辑器(例如ComboBox之类的编辑器),则需要实施IVTEditLink
并在EditLink
事件中返回OnCreateEditor
。
VST的Demo目录中有一些很好的属性编辑器示例,展示了如何实现自己的编辑器。