用TreeView遇到的问题 (50分)

  • 主题发起人 主题发起人 sun2003
  • 开始时间 开始时间
S

sun2003

Unregistered / Unconfirmed
GUEST, unregistred user!
var MyNode:TTreeNode;
MyNodeData:PNodeData; PNodeData是自定义的指针类型;
MyNode:=treeview1.Selected;

MyNodeData:=Mynode.Data;

Label1.caption:=IntToStr(MyNodeData.id) 为何程序执行到这里非法关闭;
向各位大虾请教。
 
MyNodeData^.id呢?
 
Label1.caption:=IntToStr(integer(MyNodeData))
 
unit URecorder;

interface
type
PNodeData=^TnodeData;
TNodeData=record
Id:integer;
end;

implementation

end.
 
成MyNodeData^.Nodeid 了
 
To yzhshi:MyNodeData指针定义肯定没问题的。
 
不是指针定义的问题,是如何访问,需要加上^访问吧。
 
加上^有何意义呢?
 
因为是指针,P类型,不是结构T类型,你实验一下。ok ?
 
type
PMyRec = ^TMyRec;
TMyRec = record
FName: string;
LName: string;
end;

Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.

procedure TForm1.Button1Click(Sender: TObject);

var
MyRecPtr: PMyRec;
TreeViewIndex: LongInt;
begin
New(MyRecPtr);
MyRecPtr^.FName := Edit1.Text;
MyRecPtr^.LName := Edit2.Text;
TreeViewIndex := StrToInt(Edit3.Text);
with TreeView1 do
begin
if Items.Count = 0 then
Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
elseif (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);

end;
end;

After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label.

procedure TForm1.Button2Click(Sender: TObject);

begin
Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
PMyRec(TreeView1.Selected.Data)^.LName;
end;

用AddObject生成树!
 
yzhshi的没错,但我的也可以呀!
 
多人接受答案了。
 
id是指针的内在属性吗?
还是你的指针类型中一个定义的项?
我用treeview的ttreenode.data指针时好像没用过id,
学习。
 
后退
顶部