Recent

Author Topic: [SOLVED] Stringgrid searching and scrolling  (Read 7622 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
[SOLVED] Stringgrid searching and scrolling
« on: March 07, 2017, 05:24:18 pm »
Hi there everyone,

I have a stringgrid with ID and Names in the first 2 columns. I have a combobox on top of it where I allow the user to search by ID or by Name (actually all searches are done by ID because the ID corresponding to the name is retrieved from the combobox before starting the search).

When the ID is found in the grid, the corresponding row is highlighted.

The problem is the stringgrid is starting to fill up and sometimes the highlighted row is off-screen and can only be located by scrolling up or down in the grid.

How can I set it up so that the stringgrid jumps/scrolls to highlighted row immediately after a matching ID is found?

My code is shown below:

Code: Pascal  [Select][+][-]
  1. // Searches for the name or ID of a person in a stringgrid row
  2. //
  3. // ID and Names are in the first 2 columns (ID - column 0; Names - column 1)
  4. //
  5. // Search is done on column zero (the ID column)
  6. //
  7. procedure TfrmMain.spedLookupClick(Sender: TObject);
  8. var
  9.   intID, intCount: integer;
  10.   strParam: string;
  11.   iFound: boolean;
  12. begin
  13.   // Ensure that the Lookup combobox is not empty
  14.   if (Trim(cboLookup.Text) <> EmptyStr) then
  15.   begin
  16.     //
  17.     iFound := False;
  18.     //
  19.     if TryStrToInt(Trim(cboLookup.Text), intID) then
  20.       strParam := IntToStr(intID)
  21.     else
  22.       strParam := IntToStr(Integer(cboLookup.Items.Objects[cboLookup.ItemIndex]));
  23.     //
  24.     case PageControl1.ActivePage.Caption of
  25.       'Services':
  26.       begin
  27.         //
  28.         for intCount := 0 to sgrdServices.RowCount - 1 do
  29.           begin
  30.             // Search down the ID column
  31.             if sgrdServices.Cells[0, intCount] = strParam then
  32.             begin
  33.               iFound := True;
  34.               sgrdServices.Row := intCount;    // select the row where the name or ID was found
  35.               Break;
  36.             end
  37.           end;
  38.       end;    // 'Services'
  39.     //
  40.     if not iFound then
  41.       MessageDlg(strIDNomIntrouvable, mtWarning, [mbOK], 0);
  42.   end;      // if (Trim(cboLookup.Text) <> EmptyStr) then
  43. end;
  44.  

Thanks for your contributions

JD
« Last Edit: March 07, 2017, 08:35:10 pm by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

balazsszekely

  • Guest
Re: Stringgrid searching and scrolling
« Reply #1 on: March 07, 2017, 05:46:35 pm »
@JD
Did you try to set the TopRow property to intCount?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Stringgrid searching and scrolling
« Reply #2 on: March 07, 2017, 05:47:28 pm »
Which version are you using? I have reported a similar issue related to smooth scrolling and variable height rows; the bug has been fixed in Laz trunk in Oct last year (http://bugs.freepascal.org/view.php?id=30678).

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Stringgrid searching and scrolling
« Reply #3 on: March 07, 2017, 06:00:03 pm »
@JD
Did you try to set the TopRow property to intCount?

TopRow seems to work nicely.

Thanks
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Stringgrid searching and scrolling
« Reply #4 on: March 07, 2017, 06:00:58 pm »
Which version are you using? I have reported a similar issue related to smooth scrolling and variable height rows; the bug has been fixed in Laz trunk in Oct last year (http://bugs.freepascal.org/view.php?id=30678).

I'm using Lazarus 1.6/FPC 3.0.0 on win32.
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

 

TinyPortal © 2005-2018