在TStringlist里面能不能在每个字符串那加条定义好的记录???(100分)

L

lwaif

Unregistered / Unconfirmed
GUEST, unregistred user!
这个功能类似
The following code defines a record type of TMyRec and a record pointer type of PMyRec.

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;
这样的功能
需要写些怎样的程序?
 
记录不行,可以写成对象不需要改什么,只需加Create和Free
 
TString.addobject()[:)]
 
可以,但要这样写:
Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex),pointer(MyRecPtr))
 
用Tlist是不是方便一些?直接加就行了!要得就是指针类型
 
to lwaif: 你自己不是已经找到答案了吗?:)

procedure AddRecord(Strings: TStringList; aTitle: string; aRec: TMyRec);
var
MyRec: ^TMyRec;
begin
New(MyRec);
MyRec:=aRec;
Strings.AddObject(aTitle,TObject(MyRec));
end;

procedure RemoveRecord(Strings: TStringList; index: integer);
var
MyRec: ^TMyRec;
begin
if (index>0) and (index<Strings.Count) then
begin
MyRec:=^TMyRec(Strings.Objects[index]);
Dispose(MyRec);
Strings.Delete(i);
end;
end;
function GetRecord(Strings: TStringList; index: integer): TMyRec;
begin
if (index>0) and (index<Strings.Count) then
begin
Result:=(^TMyRec(Strings.Objects[index]))^;
end;
end;
 
顶部