请问一个DELPHI 自定义数据结构的问题

2025-02-27 01:12:28
推荐回答(2个)
回答1:

-----------------------
Record 自定义结构,成员不能是对象
TMyFileList = class //不用class,用record也不行
Filelist,DirectoryList: TListbox;
end;
这样肯定是错的
-----------------------
直接写个类,类的使用需要先实例化
dos62 写的就是方法
-----------------------
请问我要定义一个全局的TListBox,怎么定义,谢谢

为什么用TListBox呢?它是个可视化组件,在添加数据的同时还多了一个刷新步骤,看你的意思应该就用到了它的Items,那直接用TStringList好了,比TListBox干净多啦,只保存数据

要定义全局,把它放在Public下就可以了,要使用的使用,Formx.ListBox1.items.add........

回答2:

TMyFileList = class //不用class,用record也不行
Filelist,DirectoryList: TListbox;
constructor Create;virtual;//类里用到别的类,需要自己重载构造函数和析构函数
destructor Destroy; override;
end;

var
Form1: TForm1;
temp1: TMyFileList;
implementation

{$R *.dfm}
constructor TMyFileList.Create;
begin
inherited Create;
FileList:=TListBox.Create(nil);
DirectoryList:=TListBox.Create(nil);
end;
destructor TMyFileList.Destroy;
begin
inherited;
FileList.Free;
DirectoryList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);//测试自定义类
begin
temp1:=TMyFileList.Create;
temp1.Filelist.Parent:=Form1;//parent负责显示
temp1.DirectoryList.Parent:=Form1;
temp1.Filelist.Items.Add('fdjkslasfd');
showmessage(temp1.Filelist.Items[0]);
temp1.Free;
end;