Add procedure in TStringGrid class lazarus / 31-LCL ---------------------------------------------------------------------- everton · 2012-01-24 15:49 Hi dear friends, I strongly recommend to add the follow procedure in the class TStringGrid. It has helped me a lot. It's simple and very useful. procedure StringGridInsertRow(StringGrid: TStringGrid; Index: Integer; Values: array of String); var   i1, i2: Integer; begin   if (Index < StringGrid.FixedRows) or (Index > StringGrid.RowCount) then     raise Exception.Create('Index out of bounds.');   StringGrid.RowCount := StringGrid.RowCount +1;   for i1 := StringGrid.RowCount -1 downto Index +1 do     for i2 := 0 to StringGrid.ColCount -1 do       StringGrid.Cells[i2, i1] := StringGrid.Cells[i2, i1 -1];   for i2 := 0 to StringGrid.ColCount -1 do     StringGrid.Cells[i2, Index] := Values[i2]; end; I don't know who does or where is supposed to put that kind of contribution, so I've done here. I hope is right. ---------------------------------------------------------------------- everton · 2012-01-26 10:22 Who can make that kind of update? ---------------------------------------------------------------------- BigChimp · 2012-01-26 10:34 Submit it to the bugtracker as a patch; Lazarus doesn't have a separate patches category in the bug tracker (FPC does), so you might precede your bug report title with [Patch] to show you've got a patch for review... See also: http://wiki.freepascal.org/How_do_I_create_a_bug_report http://wiki.freepascal.org/Creating_A_Patch ---------------------------------------------------------------------- Bart · 2012-01-26 13:44 I seem to remeber that TStringGrid already has the capability of adding and deleting rows (in contrast to my old Delphi 3, which was the reason I switched that app to lazarus in the first place). Bart ---------------------------------------------------------------------- ludob · 2012-01-26 14:06 Quote from: Bart on January 26, 2012, 02:44:59 pmI seem to remeber that TStringGrid already has the capability of adding and deleting rows (in contrast to my old Delphi 3, which was the reason I switched that app to lazarus in the first place). Bart Yep. procedure InsertColRow(IsColumn: boolean; index: integer); procedure DeleteColRow(IsColumn: Boolean; index: Integer); procedure DeleteCol(Index: Integer); virtual; procedure DeleteRow(Index: Integer); virtual; ---------------------------------------------------------------------- everton · 2012-01-27 09:27 Doesn`t have the useful of pass the values, but in that case is preferred to add a method like: InsertRow (Index: Integer; Values: array of String); that uses the InsertColRow. Anyone has any complains about to make that? Complains like make that kind of useful procedure would make the compiler slower, or the binary bigger, something like that? ---------------------------------------------------------------------- ludob · 2012-01-27 11:07 Using existing procedures where possible is making maintenance easier and avoids creating new strings to translate ('Index out of bounds.'). Other than that, the code isn't really production ready for a library: Code: [Select]  for i2 := 0 to StringGrid.ColCount -1 do     StringGrid.Cells[i2, Index] := Values[i2]; Add at least a check that Values contains StringGrid.ColCount elements before copying them. A sigsegv in a library function isn't very nice ---------------------------------------------------------------------- everton · 2012-01-27 12:30 Add a check would create a string to translate. How about: procedure StringGridInsertRow(StringGrid: TStringGrid; Index: Integer; Values: array of String; Default: String = ''); var   i1: Integer; begin   StringGrid.InsertColRow(False, Index);   for i1 := 0 to Length(Values) -1 do     StringGrid.Cells[i1, Index] := Values[i1];   for i1 := Length(Values) to StringGrid.ColCount -1 do     StringGrid.Cells[i1, Index] := Default; end; ---------------------------------------------------------------------- ludob · 2012-01-27 12:47 Code: [Select]for i1 := 0 to Length(Values) -1 do     StringGrid.Cells[i1, Index] := Values[i1]; Length(Values) > StringGrid.ColCount = trouble.  Library functions have to be fool proof. ---------------------------------------------------------------------- everton · 2012-01-27 12:59 Hehe, how about: procedure StringGridInsertRow(StringGrid: TStringGrid; Index: Integer; Values: array of String; Default: String = ''); var   i1: Integer; begin   StringGrid.InsertColRow(False, Index);   if Length(Values) > StringGrid.ColCount then     StringGrid.ColCount := Length(Values);   for i1 := 0 to Length(Values) -1 do     StringGrid.Cells[i1, Index] := Values[i1];   for i1 := Length(Values) to StringGrid.ColCount -1 do     StringGrid.Cells[i1, Index] := Default; end; After all if his setting a value he want's the value. ---------------------------------------------------------------------- ludob · 2012-01-27 15:08 That's much better ---------------------------------------------------------------------- everton · 2012-01-27 17:42 Quote from: BigChimp on January 26, 2012, 11:34:15 amSubmit it to the bugtracker as a patch; Lazarus doesn't have a separate patches category in the bug tracker (FPC does), so you might precede your bug report title with [Patch] to show you've got a patch for review... See also: http://wiki.freepascal.org/How_do_I_create_a_bug_report http://wiki.freepascal.org/Creating_A_Patch I`ll see if I understand the structure in grids.pas to do that. ---------------------------------------------------------------------- everton · 2012-01-27 18:12 Patch done. First one. So sorry by any mistakes. Is here: http://bugs.freepascal.org/view.php?id=21176