我有一种记录类型.我想在TListbox中添加该记录类型的变量,并在TListbox的click事件中检索它.可能吗?如果是,那怎么样?
我用它添加了它
lstbox.AddItem(data,myrec);
Run Code Online (Sandbox Code Playgroud)
它显示不兼容类型的错误.data是字符串,myrec是我创建的MyRecord的变量.
添加:
New(fptr1);
ZeroMemory(fptr1,sizeof(fptr1^));
fptr1^ := fptr^;
lstboxLeft.AddItem(path,TObject(fptr1));
Run Code Online (Sandbox Code Playgroud)
用于检索:
fptr := PData(lstboxLeft.Items[lstboxLeft.ItemIndex]);
Run Code Online (Sandbox Code Playgroud)
Vil*_*nde 12
AddItem将TObject作为第二个参数,而记录不是对象.您可以将记录改为TObject(通常是最佳解决方案),也可以将记录转换为对象.
这是一个使用记录的工作示例:
type
PMyRec = ^TMyRec;
TMyRec = record
I : integer;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
P : PMyRec;
begin
New(P);
P.I := 42;
ListBox1.AddItem('One',TObject(P));
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
I : integer;
begin
//Free memory
for I := 0 to ListBox1.Items.Count - 1 do
Dispose(PMyRec(ListBox1.Items.Objects[I]));
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
P : PMyRec;
begin
P := PMyRec(ListBox1.Items.Objects[ ListBox1.ItemIndex ]);
ShowMessage( IntToStr(P.I) );
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6537 次 |
最近记录: |