* * *

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

JD

  • Hero Member
  • *****
  • Posts: 1581
[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 (10, 7) - Lazarus 1.8RC4/FPC 3.0.4 (also Delphi occasionally)
Linux Mint Cinnamon  - Lazarus 1.6.4/FPC 3.0.2
Indy 10.6 series; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB

GetMem

  • Hero Member
  • *****
  • Posts: 2216
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: 3667
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).
Lazarus trunk / fpc 3.0.0 / Win32

JD

  • Hero Member
  • *****
  • Posts: 1581
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 (10, 7) - Lazarus 1.8RC4/FPC 3.0.4 (also Delphi occasionally)
Linux Mint Cinnamon  - Lazarus 1.6.4/FPC 3.0.2
Indy 10.6 series; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB

JD

  • Hero Member
  • *****
  • Posts: 1581
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 (10, 7) - Lazarus 1.8RC4/FPC 3.0.4 (also Delphi occasionally)
Linux Mint Cinnamon  - Lazarus 1.6.4/FPC 3.0.2
Indy 10.6 series; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus