Recent

Author Topic: descendent custom tstringgrid  (Read 1926 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
descendent custom tstringgrid
« on: July 28, 2021, 09:03:00 am »
Hi everyone,

I have stuck at a strange problem. Actually i am creating a custom grid which descend from TStringGrid. Everything was going smooth when I got stuck.

I want to override OnSelectCell event in my custom grid. So that if the Cell is empty then the cell will not be selected, without any user code in the form.

I am bit confused on what function to override to achieve this. Will i have to check for empty row/coumn in keydown, keyup event or have to override OnSelectCell event.

Code: Pascal  [Select][+][-]
  1.  TABCGrid = class(TStringGrid)
  2.   private
  3.   protected
  4.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
  5.     //procedure SelectCell(aCol, aRow: LongInt); override;
  6.     function SelectCell(aCol, aRow: LongInt): boolean; override;
  7.   public
  8.     constructor Create(AOwner: TComponent); override;
  9.   end;
  10.  

When overriding SelectCell entire ide freezes and have to terminate the process of lazarus.

Code: Pascal  [Select][+][-]
  1.   Result:=inherited SelectCell(ACol, ARow);
  2.   If (Cells[ACol, ARow].Trim.IsEmpty) Then Begin
  3.     Result := False;
  4.     if Assigned(OnSelectCell) then OnSelectCell(Self, aCol, aRow, Result);
  5.   end else begin
  6.       if Assigned(OnSelectCell) then OnSelectCell(Self, aCol, aRow, Result);
  7.   end;  
  8.  
« Last Edit: July 28, 2021, 04:16:10 pm by Deepaak »
Holiday season is online now. :-)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: descendent custom tstringgrid
« Reply #1 on: July 28, 2021, 09:55:18 am »
A couple little things come inmediately to mind: first, why do you call it TCustomGrid? That will override the TCustomGrid from Grids unit

Second, you're declaring SelectCell as a procedure, but it's a function in the base class, which means that you're actually "hiding" (or overloading, I'm not sure which) rather than overriding (OK if is's intentional) and that all your:
Code: Pascal  [Select][+][-]
  1. Result := whatever;
inside it are doing nothing ... indeed they should be causing compilation errors :o

Might be that is what is happening? Sometimes the IDE gets a little lost and doesn't brings to front the message window or fails to select the line with the error in the editor ... though TBH it can't be confused with a frozen IDE :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: descendent custom tstringgrid
« Reply #2 on: July 28, 2021, 04:25:19 pm »
Firstly Sorry! It was typo as I wrote from mobile  :-[
Code is rectified.

I just want to implement the same feature that is provided in OnSelectCell(Sender, col,row, canfocus)

Whenever the cell is empty then the cell must be skipped just like CanFocus does.

I want to override this feature in my custom grid, so that this operation will become transparent.
Holiday season is online now. :-)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: descendent custom tstringgrid
« Reply #3 on: July 28, 2021, 05:26:51 pm »
Ah, OK. In that case, call neither the inherited SelectCell or OnSelectCell if the cell is empty:
Code: Pascal  [Select][+][-]
  1.   Result := not Cells[ACol, ARow].Trim.IsEmpty
  2.             and inherited SelectCell(ACol, ARow);
In the normal state, with short-circuit evaluation on, that will first check whether:
Cells[ACol, ARow].Trim.IsEmpty = True
and if so, because the not, it'll result False, in which case the and would result in False and there is no need to evaluate (or execute) the inherited SelectCell().

Also, there is no need to test and call OnSelectCell since the ancestor SelectCell() in TCustomDrawGrid already does it.

Note that I have not tested this so it might not work ... though it should if all else is right.

HTH!
« Last Edit: July 28, 2021, 05:29:02 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: descendent custom tstringgrid
« Reply #4 on: July 28, 2021, 06:00:13 pm »
I will test the code and update.

Update 1: Tested the code, but when the component is inserted in the form, then whole ide freezes and have to terminate .
« Last Edit: July 29, 2021, 06:10:11 am by Deepaak »
Holiday season is online now. :-)

 

TinyPortal © 2005-2018