Recent

Author Topic: TStringGrid - how to disable Auto-selected first row  (Read 1977 times)

Napierite

  • Newbie
  • Posts: 1
TStringGrid - how to disable Auto-selected first row
« on: November 26, 2023, 10:15:54 am »
Hello all - Noob question

I have a string grid which for some reason will auto-select the first row when I Run.

I don't want any selection (only that done by a user).

So far these are the options set so not sure how these interact (have been trying diff combinations):
 [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRowSelect,goSmoothScroll]

Cheers for any help

jamie

  • Hero Member
  • *****
  • Posts: 7516
Re: TStringGrid - how to disable Auto-selected first row
« Reply #1 on: November 26, 2023, 05:02:14 pm »
you need another control that takes focus coming before the grid like a button etc.
if set the focused := false; maybe

The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13350
Re: TStringGrid - how to disable Auto-selected first row
« Reply #2 on: November 26, 2023, 05:30:23 pm »
There is no "unselected" state in the StringGrid. Even if I try to move the selected cell outside the grid (StringGrid1.Col := -1, StringGrid1.Row := -1) the selection stays at the first selectable cell.

But maybe your problem is that with goRowSelect in the grid's Options an entire row is highlighted. Do you really need this? Check out what happens when you remove this option.

jamie

  • Hero Member
  • *****
  • Posts: 7516
Re: TStringGrid - how to disable Auto-selected first row
« Reply #3 on: November 26, 2023, 06:24:32 pm »
I do notice that even though the stringgrid didn't get focused, there is a OnSelection Event. Maybe this is the issue at hand.

So Do this for example.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  2.   var CanSelect: Boolean);
  3. begin
  4.   CanSelect := TStringGrid(Sender).Focused;
  5. end;                        
  6.  
  7.  
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: TStringGrid - how to disable Auto-selected first row
« Reply #4 on: November 26, 2023, 06:36:42 pm »
Done.

Not perfect but this is the best I can do:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, Grids, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     StringGrid1: TStringGrid;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure StringGrid1PrepareCanvas(Sender: TObject; aCol, aRow: Integer;
  20.       aState: TGridDrawState);
  21.   private
  22.     FHide: Boolean;
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.   FHide := not(FHide);
  37.   StringGrid1.Invalidate;
  38. end;
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   FHide := False;
  43.   StringGrid1.Options := StringGrid1.Options + [goRowSelect];
  44. end;
  45.  
  46. procedure TForm1.StringGrid1PrepareCanvas(Sender: TObject; aCol, aRow: Integer;
  47.   aState: TGridDrawState);
  48. begin
  49.   if (gdSelected in aState) and FHide then
  50.   begin
  51.     StringGrid1.Canvas.Font.Color := clDefault;
  52.     StringGrid1.Canvas.Brush.Color := clDefault;
  53.   end;
  54. end;
  55.  
  56. end.

jamie

  • Hero Member
  • *****
  • Posts: 7516
Re: TStringGrid - how to disable Auto-selected first row
« Reply #5 on: November 26, 2023, 06:57:19 pm »
I see there is a bug in that control.
The Selected Rectangle will always draw itself on the first cell regardless of any safeguards whenever the grid gains focus.

Even using the OnSelection event, the cells still get selected but the highlighter for the cell remains on the first cell even when you disallow the cell being selected.

 So it appears that behind the scenes setting the cell via code draws the outline on the first cell before it actually goes out and checks the event to cancel it.


 
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: TStringGrid - how to disable Auto-selected first row
« Reply #6 on: November 26, 2023, 07:00:07 pm »
I knew too, that's why I said not perfect.

That is focus rectangle. Napierite asked to hide the selection, he didn't say anything about focus. But for his case, he can enable to show selection when the StringGrid get focus (OnEnter event) because he said user is allowed to make selection.
« Last Edit: November 26, 2023, 07:07:23 pm by Handoko »

 

TinyPortal © 2005-2018