【问题标题】:Replacing stringgrid with listbox in Delphi在Delphi中用列表框替换stringgrid
【发布时间】:2012-01-04 16:46:21
【问题描述】:

我正在尝试将stringgrid1stringgrid2 分别替换为listbox1listbox2。他们有什么办法我可以做到吗?如果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;

【问题讨论】:

  • 由于您有多个列,因此您需要vsReport 视图样式中的TListView 而不是列表框。
  • 怎么能把它放到我的代码里?直接替换它
  • 查看这些示例,并阅读文档。 ListItemSubItemsListItemsInsert
  • 欢迎堆栈溢出。很高兴您发布了一些代码,但是下次您提出问题时,请详细说明您要对代码执行的操作。例句:“我想显示文件夹和文件大小,还想在第三列显示一些图片,但我不知道如何使用 TStringGrid 来做到这一点”。
  • 重新发布 David 的链接并修复第二个链接(以防万一):ListItemSubItemsListItemsInsert

标签: delphi


【解决方案1】:

使用TListView 而不是TStringGrid。将您的 TStringGrid 组件替换为 TListView 组件,将其 ViewStyle 设置为 vsReport,根据需要设置其 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 来将ListViews 置于虚拟状态,从而更进一步模式,然后使用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;

【讨论】:

  • 我真的很想对此投反对票,原因很简单,您使用with 向新程序员说明了这一点。 :) (我不是,顺便说一句。)IMO 最好展示如何正确地做到这一点(没有with),以确保你不会为没有问题的人引入难以追踪的问题解决它们的经验。 (或者至少解释一下潜在的问题并建议采取不同的做法。)
  • @Remy Lebeau - TeamB 我如何将他们的 ViewStyle 设置为 vsReport...我一直在尝试但我没有实现它
  • ViewStyle 在设计时删除并选择TListView 时可在对象检查器中访问。也可以在代码中设置,例如:ListView1.ViewStyle := vsReport;.
  • @Remy Lebeau - TeamB 我完全按照你告诉我的去做,但它给了我一个错误,比如(预期的'='但'('找到)它在程序 TForm2.ListView2DblClick(发件人:TObject);
【解决方案2】:

如果您不喜欢 StringGrid,您可以使用具有报表样式和多列的 TListView。

【讨论】:

  • 我怎样才能做到这一点...我应该更换它
  • 对于 OP 的目的是探索和学习 delphi,这可能是最简单的方法,因为它不需要安装任何第三方控件。
【解决方案3】:

实际上,您可以在列表框中显示网格数据,但这不是新手的练习。该技术基于LB_SETTABSTOPS 消息处理并在Ray Konopka's book 中进行了描述。使用 ListView 是更简单的选择。

【讨论】:

    【解决方案4】:

    列表框旨在保存枚举值(省份、信用卡类型或性别)的单个垂直列表,而不是带有标题的多列显示。

    如果你想要更好的东西,你应该使用更强大的网格组件,而不是列表框。

    您也可以使用 TListView,但我不建议新手使用这种方法。我刚刚花了很多时间在视图样式模式“vsReport”下使用 TListView,我发现它比 TStringGrid 更受限制,例如,它不提供就地编辑支持。

    相反,对于新人,我建议您继续使用 TStringGrid,直到您需要做的事情(您尚未指定!)而 TStringGrid 无法完成,因为对于琐碎的代码,您已经如图所示,似乎 TStringGrid 完全符合您的要求,因此听起来您在这里毫无益处地工作。

    你到底想做什么?您想要更改控件的 TStringGrid 代码有什么问题?

    【讨论】:

    • 因此,另一种方法是选择不同的网格控件,安装它,然后更改您的代码。试试看,然后发布你能走多远。那里有许多不同的网格控件,但没有多少是免费和开源的。试试这个,例如:flex-graphics.com/Files/Free/GridView.zip
    【解决方案5】:

    随着时间的推移 - 您可能想要添加更多功能。 最好的将是 VirtualTreeView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多