Recent

Author Topic: Displaying the last row of a TStringGrid  (Read 29313 times)

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
Displaying the last row of a TStringGrid
« on: November 08, 2007, 01:51:23 pm »
I wasn't sure how to scroll a TStringGrid programmatically so the last row is visible after filling it. I found that setting the Row property to the last row number works. However the vertical scroll bar is still left at the top position. Is this a bug, or is there a better way to scroll a string grid?
Julian

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Displaying the last row of a TStringGrid
« Reply #1 on: November 08, 2007, 02:56:26 pm »
Why do you want to scroll it programmatically?

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Displaying the last row of a TStringGrid
« Reply #2 on: November 10, 2007, 09:53:02 am »
I want to show the latest entry after I've filled it, which is at the bottom.
Julian

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Displaying the last row of a TStringGrid
« Reply #3 on: November 10, 2007, 05:47:31 pm »
Do you want to fill it programmatically?

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Displaying the last row of a TStringGrid
« Reply #4 on: November 10, 2007, 08:54:04 pm »
Yes, it's basically a log, listed in increasing date order. After I fill it when the program starts, I want to scroll to show the last entry, and I will do this each time a new entry is added as well.
Julian

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Displaying the last row of a TStringGrid
« Reply #5 on: November 10, 2007, 09:20:39 pm »
You could choose an event which is fired when you fill a cell, test the events.

Then you could put on it something like:
Code: [Select]

stringgrid.col := 1;
stringgrid.row := stringgrid.rowcount - 1;

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Displaying the last row of a TStringGrid
« Reply #6 on: November 11, 2007, 02:21:55 pm »
I know. The problem is that the slider of the vertical scroll bar does not go to the bottom of the track when scrolling the grid programmatically in this way. It is not very noticeable when adding one line, but after you have filled the grid with several hundred lines and scrolled to the end the slider is still at the top.
Julian

jesusr

  • Sr. Member
  • ****
  • Posts: 484
RE: Displaying the last row of a TStringGrid
« Reply #7 on: November 16, 2007, 01:36:43 pm »
I did a small test (under windows) with a timer appending a row followed by setting the row to the last one and the scrollbar seems to be working fine, do you have an example so I can try here? what OS/Widgetset? jesusrmx a yahoo.com.mx

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Displaying the last row of a TStringGrid
« Reply #8 on: November 16, 2007, 05:58:49 pm »
This is Windows XP. I will try and come up with a simple example that demonstrates the problem.
Julian

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
Displaying the last row of a TStringGrid
« Reply #9 on: November 16, 2007, 06:20:30 pm »
OK, this shows the flicker. It's quite long because it includes the code for taking care of the unwanted line endings. It also has some code to support backspacing which is unnecessary for this test since the random character generator won't create a backspace.

The memo should be created with a vertical scroll bar. I set the timer interval to 200 so it didn't take too long to fill up the space in the memo...

Code: [Select]
var
  addcr: boolean;

procedure AddText( memo: TMemo; s: string );
var
  i: integer;
  txt: string;
  bksp: boolean;
begin
  txt := memo.Text;
  i := Length(txt);
  if (i > 0) and not addcr then
    while (i > 0) and (txt[i]<#32) do
    begin
      Delete(txt,i,1);
      Dec(i);
    end;
  if Length(s) > 0 then
  begin
    addcr := (s[1] = #13) or (s[1] = #10);
    bksp := s[1] = #8;
  end;
  if bksp then
    Delete(txt,Length(txt),1)
  else
    txt := txt + s;
  memo.Text := txt;
  memo.SelStart := Length(txt);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  c: integer;
  s: string;
begin
  c := Random(30);
  if c > 25 then s := ' ' else s := Chr(Ord('a')+c);
  AddText(Memo1,s)
end;
Julian

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Displaying the last row of a TStringGrid
« Reply #10 on: November 17, 2007, 05:11:21 am »
This is about TStringGrid or TMemo?

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
Displaying the last row of a TStringGrid
« Reply #11 on: November 17, 2007, 03:22:11 pm »
Oops, you're right. I thought this was a reply to the other thread about updating a TMemo.  :oops:

Well, for populating the string grid and then scrolling to the last row I did:

Code: [Select]
   // fill the grid
    stringgrid.RowCount := x;
    for i := 1 to x do
    begin
      stringgrid.Cells[0,i] := ...
      stringgrid.Cells[1,i] := ...
      etc
    end;
    // make the last row visible
    stringgrid.Row := x;
Julian

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Displaying the last row of a TStringGrid
« Reply #12 on: November 17, 2007, 11:42:37 pm »
Still I can't reproduce the problem under windows xp and current Lazarus and FPC versions, the better is that you submit a bug report about it and attach a compilable project, please specify the versions or your environment.

bobby100

  • Full Member
  • ***
  • Posts: 161
    • Malzilla
Displaying the last row of a TStringGrid
« Reply #13 on: November 18, 2007, 11:48:35 am »
You have property TopRow that will scroll the StringGrid. Set it to e.g. RowCount-10
https://gitlab.com/bobby100 - my Lazarus components and units
https://sourceforge.net/u/boban_spasic/profile/ - my open source apps

https://malzilla.org/ - remainder at my previous life as a web security expert

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: Displaying the last row of a TStringGrid
« Reply #14 on: April 07, 2016, 04:55:36 pm »
I apologize for posting with such a super old post, but I just ran into the exact same issue (Lazarus 1.6, Mac OS X).
This post got me in the right direction though, and instead of picking an arbitrary number, I found this to work better:


Code: Pascal  [Select][+][-]
  1. StringGrid1.TopRow:= StringGrid1.RowCount-StringGrid1.VisibleRowCount-1;


I posted this so someone else might find this useful ...

 

TinyPortal © 2005-2018