Recent

Author Topic: How to correctly set value for TStringGrid?  (Read 1682 times)

guest64953

  • Guest
How to correctly set value for TStringGrid?
« on: December 11, 2019, 01:45:49 pm »
I have a form like this. My test code successfully loaded the content of TProcess.Output into Label1 but I don't know how to make it load into TStringGrid1. Please help me, I'm very new to Lazarus  :(

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   AProcess: TProcess;
  4.   AStringList: TStringList;
  5. begin
  6.   AProcess:= TProcess.Create(nil);
  7.   AProcess.Executable:= 'zpool';
  8.   AProcess.Parameters.Add('list');
  9.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  10.   AProcess.Execute;
  11.  
  12.   AStringList:= TStringList.Create;
  13.   AStringList.LoadFromStream(AProcess.Output);
  14.   Label1.Caption:= AStringList.Text;
  15.   //StringGrid1.LoadFromStream(AProcess.Output);
  16.   AStringList.Free;
  17.   AProcess.Free;
  18. end;

p/s: Lazarus has many unfamiliar controls with me. I used to develop some hello world level applications in WInForm, I know how to use Label, Button, TextBox... but this StringGrid is very new to me. How to get documents and tutorials for this? I searched Google and Youtube without success. Sorry because I asked this very beginner question. I used to research and solve problems myself but this time I'm totally lost  :(

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: How to correctly set value for TStringGrid?
« Reply #1 on: December 11, 2019, 01:56:48 pm »

guest64953

  • Guest
Re: How to correctly set value for TStringGrid?
« Reply #2 on: December 11, 2019, 02:02:55 pm »
I don't know why you use TProcess.

I ever wrote demos about TStringGrid, I think they maybe are useful for you:
https://forum.lazarus.freepascal.org/index.php/topic,47651.msg341708.html#msg341708
https://forum.lazarus.freepascal.org/index.php/topic,37181.msg249361.html#msg249361

I want to display result of zpool list into some kind of table. The result is in this format:

Code: Pascal  [Select][+][-]
  1. NAME    SIZE  ALLOC   FREE  CKPOINT  EXPANDSZ   FRAG    CAP  DEDUP  HEALTH  ALTROOT
  2. rpool   928G  51.5G   876G

The only control I found to be looked like a table is TStringGrid. I want the table to be read only by the user, it only got updated via Get zpool list button. The user could select the zpool and I will add other buttons to get more information via zdb.

The final result is not run on OpenIndiana at all but run on Windows and Linux client, connect to the server via ssh and to be a graphical app to view and manage zpools and zfs  ;)

guest64953

  • Guest
Re: How to correctly set value for TStringGrid?
« Reply #3 on: December 11, 2019, 06:34:19 pm »
I think I should watch some Delphi tutorials on youtube to get more familiar with these controls  :D

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: How to correctly set value for TStringGrid?
« Reply #4 on: December 11, 2019, 07:21:04 pm »
Below will get you a grid populated with your sample data (you'll need to use your process call instead of my const). Some things to note is that your data had 4 spaces between each value (not sure if posting here converted tabs -> space) so that's why I used a regex to "massage" the data to a .csv. Also, there is a LoadFromCSVStream method which could probably be used, but I had already started this route before I saw it  :-[

here's the source, but I've also attached a zip and the output,
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     StringGrid1: TStringGrid;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.     procedure PopulateGrid;
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28. uses
  29.   Regex,
  30.   RegExpr,
  31.   StrUtils;
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. begin
  38.   PopulateGrid;
  39. end;
  40.  
  41. procedure TForm1.PopulateGrid;
  42. const
  43.   //simulated return data from zpool
  44.   DATA = 'NAME    SIZE  ALLOC   FREE  CKPOINT  EXPANDSZ   FRAG    CAP  DEDUP  HEALTH  ALTROOT' + sLineBreak +
  45.     'rpool   928G  51.5G   876G';
  46. var
  47.   LData : TStringList;
  48.   LSplit : TArray<String>;
  49.   LDebug: String;
  50.   I, J: Integer;
  51. begin
  52.   StringGrid1.Clear;
  53.  
  54.   LData := TStringList.Create;
  55.   try
  56.     //replace all spaces with comma
  57.     LData.Text := ReplaceRegExpr('( |\t)+', DATA, ',', True);
  58.  
  59.     //debug
  60.     LDebug := LData.Text;
  61.  
  62.     if LData.Count < 1 then
  63.       Exit;
  64.  
  65.     //add headers first
  66.     LSplit := LData[0].Split(',');
  67.     for I := 0 to High(LSplit) do
  68.       StringGrid1.Columns.Add.Title.Caption := LSplit[I];
  69.  
  70.     //add values next
  71.     StringGrid1.RowCount := Pred(LData.Count);
  72.     for I := 1 to Pred(LData.Count) do
  73.     begin
  74.       LSplit := LData[I].Split(',');
  75.       Insert('', LSplit, 0);
  76.       StringGrid1.InsertRowWithValues(I, LSplit);
  77.     end;
  78.   finally
  79.     LData.Free;
  80.   end;
  81. end;
  82.  
  83. end.
  84.  
  85.  

guest64953

  • Guest
Re: How to correctly set value for TStringGrid?
« Reply #5 on: December 13, 2019, 04:34:40 am »
^ You're real professional, man  :o

The problem with me is I can't guess the meaning of controls' name, it properties, procedures and functions so I can't figure out how to use them correctly. I don't know why but it could be because my English is not good enough. But I have no problem guessing these thing with Java Swing. I think I have to work hard to adapt myself to the Lazarus/Delphi way since I didn't have any prior experience with Delphi so I found it to be very alien to me  %)

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: How to correctly set value for TStringGrid?
« Reply #6 on: December 13, 2019, 04:50:21 am »
Hope it helps, and to be honest I don't do a lot of gui programming but did struggle a bit with what methods and properties to use in order to set this up (mentioned above about the loadfromcsv call). I can imagine that adding a language barrier on top would be pretty difficult. More online content (youtube videos, streams, reddit posts etc...) Would probably do the community good in order to onboard newcomers.

 

TinyPortal © 2005-2018