Recent

Author Topic: Add procedure in TStringGrid class  (Read 11852 times)

everton

  • Jr. Member
  • **
  • Posts: 89
Add procedure in TStringGrid class
« on: January 24, 2012, 04:49:38 pm »
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

  • Jr. Member
  • **
  • Posts: 89
Re: Add procedure in TStringGrid class
« Reply #1 on: January 26, 2012, 11:22:41 am »
Who can make that kind of update?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Add procedure in TStringGrid class
« Reply #2 on: January 26, 2012, 11:34:15 am »
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
« Last Edit: January 26, 2012, 11:36:32 am by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Bart

  • Hero Member
  • *****
  • Posts: 5663
    • Bart en Mariska's Webstek
Re: Add procedure in TStringGrid class
« Reply #3 on: January 26, 2012, 02:44:59 pm »
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

  • Hero Member
  • *****
  • Posts: 1173
Re: Add procedure in TStringGrid class
« Reply #4 on: January 26, 2012, 03:06:40 pm »
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

Yep.
procedure InsertColRow(IsColumn: boolean; index: integer);

procedure DeleteColRow(IsColumn: Boolean; index: Integer);
procedure DeleteCol(Index: Integer); virtual;
procedure DeleteRow(Index: Integer); virtual;

everton

  • Jr. Member
  • **
  • Posts: 89
Re: Add procedure in TStringGrid class
« Reply #5 on: January 27, 2012, 10:27:28 am »
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

  • Hero Member
  • *****
  • Posts: 1173
Re: Add procedure in TStringGrid class
« Reply #6 on: January 27, 2012, 12:07:50 pm »
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

  • Jr. Member
  • **
  • Posts: 89
Re: Add procedure in TStringGrid class
« Reply #7 on: January 27, 2012, 01:30:02 pm »
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

  • Hero Member
  • *****
  • Posts: 1173
Re: Add procedure in TStringGrid class
« Reply #8 on: January 27, 2012, 01:47:54 pm »
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

  • Jr. Member
  • **
  • Posts: 89
Re: Add procedure in TStringGrid class
« Reply #9 on: January 27, 2012, 01:59:44 pm »
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

  • Hero Member
  • *****
  • Posts: 1173
Re: Add procedure in TStringGrid class
« Reply #10 on: January 27, 2012, 04:08:52 pm »
That's much better  :)

everton

  • Jr. Member
  • **
  • Posts: 89
Re: Add procedure in TStringGrid class
« Reply #11 on: January 27, 2012, 06:42:24 pm »
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

I`ll see if I understand the structure in grids.pas to do that.

everton

  • Jr. Member
  • **
  • Posts: 89
Re: Add procedure in TStringGrid class
« Reply #12 on: January 27, 2012, 07:12:51 pm »
Patch done. First one. So sorry by any mistakes.

Is here: http://bugs.freepascal.org/view.php?id=21176
« Last Edit: January 28, 2012, 03:19:13 pm by everton »

 

TinyPortal © 2005-2018