Recent

Author Topic: [SOLVED] problems with HourGlass-Cursor over a TStringGrid  (Read 3836 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1047
[SOLVED] problems with HourGlass-Cursor over a TStringGrid
« on: October 07, 2016, 05:19:43 pm »
I have a TStringGrid and want to compute and show one row after each other. Because this needs some time for > 1000 rows, I will show the HourGlass-Cursor until the last row is done.

But in my program this works only, if the mouse is over the StringGrid area at the moment, when I press the button (by TAB and ENTER). When I press the button with the mouse (so the mouse is not over the StringGrid area at this moment) I don't get the HourGlass-Cursor, even if I move the mouse now over the StringGrid area.

I'm working on Windows 7 32 bit.
Can please somebody tell me, what I do wrong?
I zipped and appended my example project, if someone wants to compile and start it.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
   var n: integer;
   begin
   Stringgrid1.RowCount:=1;  {1 row for the titles}
   Stringgrid1.FixedCols:=0; {no fixed colums}
   Stringgrid1.ColCount:=1;  {have only 1 column}
   Stringgrid1.Cells[0,0]:='Results';

   Stringgrid1.Cursor:=crHourGlass; {want HourGlass-Cursor}
   for n:=1 to 10 do
      begin
      Stringgrid1.RowCount:=n+1;  {add 1 row}
      sleep(300);                 {compute something...}
      Stringgrid1.Cells[0,n]:='Hello' + intToStr(n);
      StringGrid1.Refresh;        {show the new line}
      end;
   StringGrid1.Cursor:=crDefault; {back to normal Cursor}
   end;   
« Last Edit: October 07, 2016, 06:14:23 pm by Hartmut »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: problems with HourGlass-Cursor over a TStringGrid
« Reply #1 on: October 07, 2016, 05:40:27 pm »

wp

  • Hero Member
  • *****
  • Posts: 13334
Re: problems with HourGlass-Cursor over a TStringGrid
« Reply #2 on: October 07, 2016, 05:41:31 pm »
I have a TStringGrid and want to compute and show one row after each other. Because this needs some time for > 1000 rows (...)
Why? 1000 lines add within the wink of an eye. Did you call StringGrid1.BeginUpdate before you add the cells? This will avoid the repainting operations. Afterwards you must call StringGrid1.EndUpdate in order to activate painting again.

But in my program this works only, if the mouse is over the StringGrid area at the moment, when I press the button (by TAB and ENTER). When I press the button with the mouse (so the mouse is not over the StringGrid area at this moment) I don't get the HourGlass-Cursor, even if I move the mouse now over the StringGrid area.
Use the Cursor of the Screen to avoid the button presses.

Here is my modification of your code. Note the try-finally block to make sure that the grid's EndUpdate is called and the Screen.Cursor is reset if an exception occurs:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   n: integer;
  4. begin
  5.   Stringgrid1.RowCount:=1;  {1 row for the titles}
  6.   Stringgrid1.FixedCols:=0; {no fixed colums}
  7.   Stringgrid1.ColCount:=1;  {have only 1 column}
  8.   Stringgrid1.Cells[0,0]:='Results';
  9.   Screen.Cursor := crHourGlass;
  10.   StringGrid1.BeginUpdate;
  11.   try
  12.     for n:=1 to 1000 do
  13.     begin
  14.       Stringgrid1.RowCount:=n+1;  {add 1 row}  // <-- having this outside the loop (StringGrid1.RowCount := 1001) would be faster!
  15. //      sleep(300);                 {compute something...}
  16.       Stringgrid1.Cells[0,n]:='Hello' + intToStr(n);
  17.       StringGrid1.Refresh;        {show the new line}
  18.     end;
  19.   finally
  20.     Screen.Cursor := crDefault;
  21.     StringGrid1.EndUpdate;
  22.   end;
  23. end;

(And I gave your code a more conventional indentation - I never understand why so many people use their own indentation rules. Proper indentation is the key to see missing end statements and illegal blocks immediately).
« Last Edit: October 07, 2016, 06:34:26 pm by wp »

Thaddy

  • Hero Member
  • *****
  • Posts: 18695
  • To Europe: simply sell USA bonds: dollar collapses
Re: problems with HourGlass-Cursor over a TStringGrid
« Reply #3 on: October 07, 2016, 05:44:53 pm »
@wp
You mean, start with Python and THEN switch to Object Pascal?

I fully agree about indentation.  :o 8-)
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Hartmut

  • Hero Member
  • *****
  • Posts: 1047
Re: problems with HourGlass-Cursor over a TStringGrid
« Reply #4 on: October 07, 2016, 06:14:01 pm »
To use Screen.Cursor() instead of Stringgrid1.Cursor() works perfect :-)
BeginUpdate() and EndUpdate() I did not know, but now. Thanks a lot again to wp for your help.

 

TinyPortal © 2005-2018