Recent

Author Topic: Intercept DBGrid OnColumnMove  (Read 353 times)

daveinhull

  • Full Member
  • ***
  • Posts: 184
  • 1 divided by nothing must still be 1!
Intercept DBGrid OnColumnMove
« on: January 11, 2019, 07:23:58 pm »
Hi,

I'd like to intercept the OnColumnMove event in a DBGrid, and have been reading up about Main Loop Hooks and AddEventHandler which is the way I think I need to do this. I've tried searching for some example, but can't find anything, does anyone have a pointer to some examples or a tutorial.

Just for background, I'm trying to write a set of routines that adds a dropdown filter button to the title columns in a DBGrid, so I don't just want to add my own routine to the OnColmnMoved or OnColumnSized, as the developer (me typical) may also want to use these events for other things.

Also on a similar note, does OnColumnMoved get fired after the columns have moved (as the name would suggest) or before - from what I can see it is before as everything in the grid appears the same.

Thanks in advance
Dave 
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 717
Re: Intercept DBGrid OnColumnMove
« Reply #1 on: January 11, 2019, 09:58:30 pm »
The OnColumnMoved event is published: You can set it in the Object Inspector or assign it in code like any other event.
« Last Edit: January 11, 2019, 10:00:42 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 12..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

daveinhull

  • Full Member
  • ***
  • Posts: 184
  • 1 divided by nothing must still be 1!
Re: Intercept DBGrid OnColumnMove
« Reply #2 on: January 11, 2019, 11:59:11 pm »
Thanks lucamar,

I realize that I can set a routine through the Object Inspector or through code , but I was interested in traping the event before it gets to that point. I wanted to do something in a general routine before passing the execution to that routine (if there is one).

Any thoughts
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 717
Re: Intercept DBGrid OnColumnMove
« Reply #3 on: January 12, 2019, 12:22:04 am »
Oh, ok. Only thing that I can think at the moment is to dive into the source and see where and in response to what message (if any) or condition it's being called. Once you know that you can ascertain where and how intercept it. You may have to subclass it, though.

I'll give it a look a little later (or tomorrow!).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 12..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

daveinhull

  • Full Member
  • ***
  • Posts: 184
  • 1 divided by nothing must still be 1!
Re: Intercept DBGrid OnColumnMove
« Reply #4 on: January 12, 2019, 01:14:40 am »
Hey lucamar many thanks, but please don't spend too long on it, but any clues to help are most welcome  :)

I thought I could do it by using the AddEventHandler, but I'll see if I can navigate my way through the source code as well, although it may be a little tricky for a 1980's DEC VAR Pascal programmer  :o
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

daveinhull

  • Full Member
  • ***
  • Posts: 184
  • 1 divided by nothing must still be 1!
Re: Intercept DBGrid OnColumnMove
« Reply #5 on: January 14, 2019, 12:26:57 pm »
Any thoughts anyone?
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 717
Re: Intercept DBGrid OnColumnMove
« Reply #6 on: January 14, 2019, 02:39:12 pm »

I have followed the source and I think you'll have to subclass TDBGrid and set your own handler for the method ColRowMoved. For example:
Code: Pascal  [Select]
  1. unit MyDBGrid;
  2.  
  3. interface
  4.  
  5. type
  6.   TMyDBGrid = (TDBGrid)
  7.   protected
  8.     procedure ColRowMoved(IsColumn: Boolean; FromIndex,ToIndex: Integer); override;
  9.   end;
  10.  
  11. implementation
  12.  
  13. procedure TMyDBGrid.ColRowMoved(IsColumn: Boolean; FromIndex,ToIndex: Integer);
  14. begin
  15.   {Do whatever you want before calling ...}
  16.   ShowMessage('I''m an interceptor !!!');
  17.   inherited ColRowMoved(IsColumn, FromIndex, ToIndex);
  18. end;
  19.  
  20. end.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 12..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

daveinhull

  • Full Member
  • ***
  • Posts: 184
  • 1 divided by nothing must still be 1!
Re: Intercept DBGrid OnColumnMove
« Reply #7 on: January 14, 2019, 02:54:45 pm »
Hey lucamar many thanks for this, I'll take this further and see what I can do with it.

Just one quick (well I hope it is quick) question from your investigations; would it then be possible to continue to execute any user defined procedure.

The reason for me asking this which might sound strange as I'm the user of the routine in this case, is that I want to provide routine that adds dropdown filters to the DBGrid at design time, but leave the option for the programmer (me) to also add real app events on the same event.

So for example, my 'real'app code I'm trying to develop in the future uses a DBGrid and I need in that use to catch the ColRowMoved for some purpose in the app. But I also want to call this routine I'm working on to add dropdown filters to the DBGrid. So I have an app need for the event and I also have a Dropdown filter need for the event.

Hope thi smakes sence.
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64