Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing stringgrid with listbox in Delphi

Tags:

delphi

I am trying to replace the stringgrid1 and stringgrid2 with listbox1 and listbox2 respectively. Is their any way I can do it? If listbox can not do may anyone suggest what I should use instead of stringgrid to display the information? I'm a newbie to Delphi.

This is my code:

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;

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;
like image 284
Mildred Shimz Avatar asked Jan 04 '12 16:01

Mildred Shimz


4 Answers

Use TListView instead of TStringGrid. Replace your TStringGrid components with TListView components, set their ViewStyle to vsReport, set up their Columns collections as needed, and then update your code as follows:

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;

With that said, depending on how and when TABLE[] and fat[] are actually being filled in, you might be able to take this a step farther by setting the TListView.OwnerData properties to True to put the ListViews into virtual mode, and then use the TListView.OnData event to display your data dynamically. That way, you can get rid of your HD() procedure completely, as your data does not need to be copied in the TListView itself anymore, it can be displayed from TABLE[] and fat[] directly instead, eg:

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;
like image 161
Remy Lebeau Avatar answered Nov 08 '22 03:11

Remy Lebeau


If you don't like the StringGrid you can use a TListView with report style and multiple columns.

like image 37
Marco Cantù Avatar answered Nov 08 '22 02:11

Marco Cantù


Actually you can show grid data in a listbox, but that is not an exercise for a newbie. The technique is based on LB_SETTABSTOPS message processing and described in Ray Konopka's book. Using ListView is much simpler alternative.

like image 44
kludg Avatar answered Nov 08 '22 02:11

kludg


A listbox is intended to hold a single vertical list of enumerated values (provinces, credit card types, or genders), not a multi-column display with headings.

If you want something better, you should use a more powerful grid component, not a listbox.

You could also use TListView, but I do not recommend that approach for a new person. I have just spent a lot of time working with TListView in view style mode "vsReport", and I find it is even more limited than TStringGrid, for example, it provides no in-place edit support.

Instead, for a new person, I recommend you stay with the TStringGrid until there is something you need to do (which you have not specified!) that can't be done with TStringGrid, since for the trivial code you've shown, it seems TStringGrid does exactly what you want, and so it only sounds like you're making work for no benefit here.

What are you trying to do exactly? What problem is there with you TStringGrid code that you want do do something to change the control?

like image 1
Warren P Avatar answered Nov 08 '22 02:11

Warren P