本文介绍了如何将对象添加到virtualtreeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我试图将对象添加到VirtualTreeView中,但总是在CellText上出现错误EAccessViolation,我想知道我尽力而为却没有结果的错误原因...

In short, I'm trying to add Object into VirtualTreeView but am always getting error EAccessViolation at CellText I'm wondering about the reason for the error i tried to do my best but without result...

这是我的尝试

 TForm1 = class(TForm)
 private

 public

 end;

 TPerson = class;

 PPersonNode = ^TPersonNode;
 TPersonNode = record
   PersonObj: TPerson;
 end;

 TPerson = class(TObject)
 private
   FName: string;
 public
   property Name: string read FName write FName;
 end;

 procedure TForm1.Button1Click(Sender: TObject);
 var
   PersonRec: PPersonNode;
 begin
   PersonRec := VirtualStringTree1.GetNodeData(VirtualStringTree1.AddChild(nil));
   PersonRec.PersonObj := TPerson.Create;
   PersonRec.PersonObj.Name := 'aaa';
 end;

 procedure TForm1.VirtualStringTree1GetNodeDataSize(Sender: TBaseVirtualTree;
   var NodeDataSize: Integer);
 begin
   NodeDataSize := SizeOf(TPersonNode);
end;

procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
var
   PersonRec: PPersonNode;
begin
   PersonRec := Sender.GetNodeData(Node);
   CellText := PersonRec.PersonObj.Name;
end;

推荐答案

在创建TPerson对象之前,似乎正在触发 OnGetText 事件.尝试先检查对象是否为零:

It looks like the OnGetText event is being fired before the TPerson object has been created. Try checking that the object isn't nil first:

procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
var
   PersonRec: PPersonNode;
begin
   PersonRec := Sender.GetNodeData(Node);

   if PersonRec <> nil then  // <- check that the object has been created
     CellText := PersonRec.PersonObj.Name;
end;

这篇关于如何将对象添加到virtualtreeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:29