gpt4 book ai didi

delphi - 在 VirtualTreeView 中编辑根节点单元格文本

转载 作者:行者123 更新时间:2023-12-03 15:27:32 25 4
gpt4 key购买 nike

我想更改现有虚拟(根)节点的单元格文本。我可以更改数据,但我必须刷新整个 TreeView,以便再次触发 GetText。是否有更简单的方法或可能的方法来仅刷新 1 个根节点而不是整个树?

感谢您的帮助。

最佳答案

在下面的代码中,我尝试模拟您的节点更新事件。这里用按钮点击Button1Click事件来表示。在这里,如果您单击该按钮,数据记录中的值将增加,并且节点将被强制重新绘制(这将触发 OnGetText 事件,您将在该记录中获得新增加的值准备好获取)。在您的实际代码中,您需要执行以下操作:

  • 找到您要更新的节点(我找到了 TopNode :-)
  • 更新该节点的数据记录(在我的情况下,Count 值会递增)
  • 调用该节点的 InvalidateNode(这将触发某些事件的虚拟事件链,但 OnGetText 将在其中)

type
PTreeData = ^TTreeData;
TTreeData = record
Title: string;
Count: Integer;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
TreeData: PTreeData;
TreeNode: PVirtualNode;
begin
// this is just for memory leaks report
ReportMemoryLeaksOnShutdown := True;
// initialize data size for our data record
VirtualTree.NodeDataSize := SizeOf(TTreeData);
// add one node and store it to the TreeNode variable
TreeNode := VirtualTree.AddChild(nil);
// get the pointer to just created node data record
TreeData := VirtualTree.GetNodeData(TreeNode);
// and assign some values to that data record
TreeData.Title := 'Count: ';
TreeData.Count := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
TreeData: PTreeData;
TreeNode: PVirtualNode;
begin
// now I take the the top node just as an example
TreeNode := VirtualTree.TopNode;
// get the pointer to its data record
TreeData := VirtualTree.GetNodeData(TreeNode);
// increase the value in data record for this node
TreeData.Count := TreeData.Count + 1;
// and force the node to repaint, what will except
// other events trigger also the OnGetText one
VirtualTree.InvalidateNode(TreeNode);
end;

procedure TForm1.VirtualTreeGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
var
TreeData: PTreeData;
begin
// here I'll get the pointer to data of the currently refreshed cell's node
TreeData := VirtualTree.GetNodeData(Node);
// and return the caption of the cell
CellText := TreeData.Title + IntToStr(TreeData.Count);
end;

procedure TForm1.VirtualTreeFreeNode(Sender: TBaseVirtualTree;
Node: PVirtualNode);
var
TreeData: PTreeData;
begin
// this is here because of VT memory leaks, so get the pointer to data
TreeData := Sender.GetNodeData(Node);
// and finalize them
Finalize(TreeData^);
end;

关于delphi - 在 VirtualTreeView 中编辑根节点单元格文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487300/

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