gpt4 book ai didi

delphi - 在Delphi中用列表框替换stringgrid

转载 作者:行者123 更新时间:2023-12-03 14:58:56 24 4
gpt4 key购买 nike

我试图分别用 listbox1listbox2 替换 stringgrid1stringgrid2 。他们有什么办法我可以做到吗?如果 listbox 不能做,有人建议我应该使用什么来代替 stringgrid 来显示信息?我是 Delphi 的新手。

这是我的代码:

procedure TForm2.FormCreate(Sender: TObject);
var i:integer;
begin
stringgrid1.ColWidths[0]:=20;
stringgrid2.ColWidths[0]:=20;
for i:=1 to 50 do begin
stringgrid1.Cells[0,i]:=inttostr(i-1);
stringgrid2.Cells[0,i]:=inttostr(i-1);
stringgrid2.Cells[1,i]:='0';
end;
stringgrid2.Cells[1,0]:='name';
stringgrid1.Cells[1,0]:='extension';
stringgrid1.Cells[2,0]:='format';
stringgrid1.Cells[3,0]:='size';
stringgrid1.Cells[4,0]:='date';
stringgrid1.Cells[5,0]:='addres';
end;

procedure TForm2.StringGrid2DblClick(Sender: TObject);
begin
if (stringgrid2.Cells[1,stringgrid2.Row]<>'1024') and (stringgrid2.Cells[1,stringgrid2.Row]<>'0') then
stringgrid1.Row:=strtoint(stringgrid2.Cells[1,stringgrid2.Row]);

end;

结束。

Procedure HD;
var i:integer;
begin
for i:=0 to 50 do begin
form2.StringGrid1.Cells[1,i+1]:=TABLE[i].name;
form2.StringGrid1.Cells[2,i+1]:=TABLE[i].format;
if TABLE[i].tip then
form2.StringGrid1.Cells[3,i+1]:='folder'
else
form2.StringGrid1.Cells[3,i+1]:='file';
form2.StringGrid1.Cells[4,i+1]:=inttostr(TABLE[i].nach);
form2.StringGrid1.Cells[5,i+1]:=inttostr(TABLE[i].razmer);
form2.StringGrid2.Cells[1,i+1]:=inttostr(fat[i]);;
end;
end;

最佳答案

使用TListView而不是TStringGrid 。替换您的TStringGrid组件 TListView组件,设置其 ViewStylevsReport ,设置他们的Columns根据需要收集集合,然后按如下方式更新代码:

procedure TForm2.FormCreate(Sender: TObject); 
var
i: integer;
begin
// NOTE: this can all be done at design-time so
// you don't need to do it in code at runtime!
ListView1.Colums[0].Width := 20;
ListView2.Colums[0].Width := 20;
for i := 0 to 49 do begin
ListView1.Items.Add.Caption := IntToStr(i);
with ListView2.Items.Add do begin
Caption := IntToStr(i);
SubItems.Add('0');
end;
end;
ListView2.Columns[1].Caption := 'name';
ListView1.Columns[1].Caption := 'extension';
ListView1.Columns[2].Caption := 'format';
ListView1.Columns[3].Caption := 'size';
ListView1.Columns[4].Caption := 'date';
ListView1.Columns[5].Caption := 'addres';
end;

procedure TForm2.ListView2DblClick(Sender: TObject);
var
Item: TListItem;
begin
Item := ListView2.Selected;
if Item = nil then Exit;
if (Item.SubItems[0] <> '1024') and (Item.SubItems[0] <> '0') then
ListView1.Selected := ListView1.Items[StrToInt(Item.SubItems[0])];
end;

procedure HD;
var
i: integer;
begin
for i := 0 to 49 do begin
with form2.ListView1.Items[i] do begin
SubItems[0] := TABLE[i].name;
SubItems[1] := TABLE[i].format;
if TABLE[i].tip then
SubItems[2] := 'folder'
else
SubItems[2] := 'file';
SubItems[3] := IntToStr(TABLE[i].nach);
SubItems[4] := IntToStr(TABLE[i].razmer);
end;
form2.ListView2.Items[i].SubItems[0] := IntToStr(fat[i]);
end;
end;

话虽如此,取决于方式和时间TABLE[]fat[]实际上已被填写,您可以通过设置 TListView.OwnerData 更进一步属性设置为 True 将 ListView 置于虚拟模式,然后使用 TListView.OnData动态显示数据的事件。这样,您就可以摆脱您的 HD()整个过程,因为您的数据不需要复制到TListView中它本身不再是,它可以从TABLE[]显示和fat[]直接代替,例如:

procedure TForm2.FormCreate(Sender: TObject); 
var
i: integer;
begin
// NOTE: this can all be done at design-time so
// you don't need to do it in code at runtime!
ListView1.Colums[0].Width := 20;
ListView2.Colums[0].Width := 20;
ListView2.Columns[1].Caption := 'name';
ListView1.Columns[1].Caption := 'extension';
ListView1.Columns[2].Caption := 'format';
ListView1.Columns[3].Caption := 'size';
ListView1.Columns[4].Caption := 'date';
ListView1.Columns[5].Caption := 'addres';
//

ListView1.Items.Count := 50;
ListView2.Items.Count := 50;
end;

procedure TForm2.ListView2DblClick(Sender: TObject);
var
Item: TListItem;
begin
Item := ListView2.Selected;
if Item = nil then Exit;
if (Item.SubItems[0] <> '1024') and (Item.SubItems[0] <> '0') then
ListView1.Selected := ListView1.Items[StrToInt(Item.SubItems[0])];
end;

procedure TForm2.ListView1Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := IntToStr(Item.Index);
Item.SubItems.Add(TABLE[Item.Index].name);
Item.SubItems.Add(TABLE[Item.Index].format);
if TABLE[i].tip then
Item.SubItems.Add('folder')
else
Item.SubItems.Add('file');
Item.SubItems.Add(IntToStr(TABLE[i].nach));
Item.SubItems.Add(IntToStr(TABLE[i].razmer))
end;

procedure TForm2.ListView2Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := IntToStr(Item.Index);
Item.SubItems.Add(IntToStr(fat[i]));
end;

关于delphi - 在Delphi中用列表框替换stringgrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com