来自 TVirtualStringTree 的 GetText 被触发更多次
GetText from TVirtualStringTree is getting triggered more times
请看下面的代码:
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VirtualTrees, Vcl.StdCtrls;
type
TTreeData = record
Fields: array of String;
end;
PTreeData = ^TTreeData;
const
szVirtualTree = SizeOf(TTreeData);
type
TForm2 = class(TForm)
tree: TVirtualStringTree;
Button1: TButton;
procedure treeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
procedure treeGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
LTreeData: PTreeData;
Node: PVirtualNode;
cols, rows, col, row: Integer;
begin
cols:= 2;
rows:= 2;
Tree.Header.Columns.Clear;
if cols > 0 then
for col := 0 to cols - 1 do
with Tree.Header.Columns.Add do
begin
Text:= 'H' + IntToStr(col);
Width:= 80;
end;
Tree.Clear;
Tree.BeginUpdate;
if (cols> 0) and (rows > 0) then
for row := 0 to rows - 1 do
begin
Node:= Tree.AddChild(nil,nil);
Tree.ValidateNode(Node,False);
LTreeData:= Tree.GetNodeData(Node);
SetLength(LTreeData^.Fields, cols);
if cols > 0 then
for col := 0 to cols - 1 do
LTreeData^.Fields[col]:= '[' + inttostr(col) + ',' + inttostr(row) + ']';
end;
Tree.EndUpdate;
end;
procedure TForm2.treeGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
begin
NodeDataSize:= szVirtualTree;
end;
procedure TForm2.treeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
LTreeData: PTreeData;
begin
LTreeData:= Sender.GetNodeData(Node);
CellText:= LTreeData^.Fields[Column];
showmessage(CellText);
end;
end.
按下按钮后,我收到的消息是:
[0,0]
[0,0]
[0,0]
[0,0]
[1,0]
[1,0]
[1,0]
[1,0]
[0,1]
[0,1]
[0,1]
[0,1]
[1,1]
[1,1]
[1,1]
[1,1]
基本上 GetText
每个节点和每个列被触发 4 次。
为什么不是只触发一次?像这样:[0,0] [1,0] [0,1] [1,1]
控件使用所谓的虚拟范例。作者 Mike Lischke explains this like so:
As the name already indicates, this control uses a different paradigm for tree management than other controls of this kind. It does not know anything about the data it manages (except its size), not even the captions of a node. Everything is retrieved from the application via events (or descendants via overridden methods).
控件不跟踪节点标题。每当它需要知道它们时,它就会触发 OnGetText
方法。该控件不缓存,不记住节点标题。因此,如果控制代码的多个部分依赖于标题,那么将为同一节点多次调用该事件。
您观察到的行为完全正常,完全可以预料。 OnGetText
处理程序的工作是生成节点的文本。如果同一个问题被问了两次,请不要担心。只要确保您每次都给出相同的答案即可!
请看下面的代码:
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VirtualTrees, Vcl.StdCtrls;
type
TTreeData = record
Fields: array of String;
end;
PTreeData = ^TTreeData;
const
szVirtualTree = SizeOf(TTreeData);
type
TForm2 = class(TForm)
tree: TVirtualStringTree;
Button1: TButton;
procedure treeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
procedure treeGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
LTreeData: PTreeData;
Node: PVirtualNode;
cols, rows, col, row: Integer;
begin
cols:= 2;
rows:= 2;
Tree.Header.Columns.Clear;
if cols > 0 then
for col := 0 to cols - 1 do
with Tree.Header.Columns.Add do
begin
Text:= 'H' + IntToStr(col);
Width:= 80;
end;
Tree.Clear;
Tree.BeginUpdate;
if (cols> 0) and (rows > 0) then
for row := 0 to rows - 1 do
begin
Node:= Tree.AddChild(nil,nil);
Tree.ValidateNode(Node,False);
LTreeData:= Tree.GetNodeData(Node);
SetLength(LTreeData^.Fields, cols);
if cols > 0 then
for col := 0 to cols - 1 do
LTreeData^.Fields[col]:= '[' + inttostr(col) + ',' + inttostr(row) + ']';
end;
Tree.EndUpdate;
end;
procedure TForm2.treeGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
begin
NodeDataSize:= szVirtualTree;
end;
procedure TForm2.treeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
LTreeData: PTreeData;
begin
LTreeData:= Sender.GetNodeData(Node);
CellText:= LTreeData^.Fields[Column];
showmessage(CellText);
end;
end.
按下按钮后,我收到的消息是:
[0,0]
[0,0]
[0,0]
[0,0]
[1,0]
[1,0]
[1,0]
[1,0]
[0,1]
[0,1]
[0,1]
[0,1]
[1,1]
[1,1]
[1,1]
[1,1]
基本上 GetText
每个节点和每个列被触发 4 次。
为什么不是只触发一次?像这样:[0,0] [1,0] [0,1] [1,1]
控件使用所谓的虚拟范例。作者 Mike Lischke explains this like so:
As the name already indicates, this control uses a different paradigm for tree management than other controls of this kind. It does not know anything about the data it manages (except its size), not even the captions of a node. Everything is retrieved from the application via events (or descendants via overridden methods).
控件不跟踪节点标题。每当它需要知道它们时,它就会触发 OnGetText
方法。该控件不缓存,不记住节点标题。因此,如果控制代码的多个部分依赖于标题,那么将为同一节点多次调用该事件。
您观察到的行为完全正常,完全可以预料。 OnGetText
处理程序的工作是生成节点的文本。如果同一个问题被问了两次,请不要担心。只要确保您每次都给出相同的答案即可!