gpt4 book ai didi

delphi - TVirtualStringTree 中的 GetText 被触发更多次

转载 作者:行者123 更新时间:2023-12-01 23:31:34 24 4
gpt4 key购买 nike

请看下面的代码:

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]

enter image description here

最佳答案

该控件使用所谓的虚拟范例。作者 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 处理程序的工作是生成节点的文本。如果同一问题被问两次,请不要担心。只要确保每次都给出相同的答案即可!

关于delphi - TVirtualStringTree 中的 GetText 被触发更多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com