Recent

Author Topic: Title with filter box  (Read 4768 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1883
    • Lazarus and Free Pascal italian community
Title with filter box
« on: January 08, 2019, 11:26:04 am »
Hi guys, I wanted to ask you something. What if I want to add a TEditBox or any other component to a cell that is the title of a column in a dbgrid?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #1 on: January 09, 2019, 03:03:22 pm »
Hi Same question here from me a few weeks ago:
http://forum.lazarus.freepascal.org/index.php/topic,43711.0.html

I'ev not had much luck so far so if you get anywhere please share, thanks.

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

wp

  • Hero Member
  • *****
  • Posts: 5740
Re: Title with filter box
« Reply #2 on: January 09, 2019, 03:36:25 pm »
  • Add a TEdit to the form
  • Set its Visible to false
  • In OnCreate of the form set its Parent to the DBGrid.
  • When the Edit should be shown you call
Code: Pascal  [Select]
  1.   Edit1.BoundsRect := DBGrid1.CellRect(2, 0);  // I am assuming here that the Edit should be in col=2
  2.   Edit1.Show;  
  • Since, on Windows, the height of the Edit does not change, you should set the DefaultRowHeight of the DBGrid to the Height of the Edit: DBGrid1.DefaultRowheight := Edit1.Height+1
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #3 on: January 09, 2019, 04:03:57 pm »
Hi, I've done that, but what I've got a problem with is knowing when to show the edit box (or a button).
I was trying to show it using OnDrawColumnCell but this puts it in all rows.

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

wp

  • Hero Member
  • *****
  • Posts: 5740
Re: Title with filter box
« Reply #4 on: January 09, 2019, 04:16:36 pm »
I've got a problem with is knowing when to show the edit box (or a button).
If *you* don't know how should *I* know? What is the edit/button good for? What is the user supposed to do to make it appear?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #5 on: January 09, 2019, 08:01:04 pm »
WP, TI'm not wanting the user to decide when to show the button I want it to appear when the DBGrid is drawn. Please see http://forum.lazarus.freepascal.org/index.php/topic,43711.0.html.

As I said in the post I was trying to use the OnDrawColumnCell to draw the button, did you see that or where you too busy having a go at me?

I wanted top put a button at the top of each column so that the user can press it and get the option to sort and filter on that column. When the OnDrawColumnCell triggers I need to see which row it is drawing and if row 1 (the title row) then I wanted to also draw the button control in the Cell.Rect.
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: 1610
Re: Title with filter box
« Reply #6 on: January 09, 2019, 08:29:47 pm »
If you only want it in the Title row then use the OnDrawColumnTitle event, not the OnDrawColumCell.

You should at least skim the docs. Granted that there isn't much on them but the names of proerties, methods and events are quite evocative of what they are meant for. And check the examples. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #7 on: January 09, 2019, 08:37:33 pm »
Thanks lucamar, I have been trying to find documentation, and the first place I start with is in the Object Inspector, but that event isn't listed!

@wp, anyway, thanks for the pointer to CellRect, which  I'd missed.
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: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #8 on: January 09, 2019, 08:47:33 pm »
lucamar, honest I do look at documentation when I can find it  ;)

I've just try doing a search for "OnDrawColumnTitle" both in google using Lazarus PAscal OnDrawColumnTitle and in the on-line help and I can't find anything.

I've got it working now using DrawColumnCell and wp pointer to CellRect, although I'm not sure it is the best way, but it does what I want.

Thanks both.
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: 1610
Re: Title with filter box
« Reply #9 on: January 09, 2019, 09:21:27 pm »
If you have the help installed (the CHM files), try searching TDBGrid, then click the "Events" link and there you have it.

The difference with OnDrawColumnCell is that OnDrawColumnTitle is triggered just to draw the title cells, not for all cells in the grid. This not only makes it a more efficient solution but you wont' have to check whether you're in the correct row: you know you are.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #10 on: January 12, 2019, 12:26:20 pm »
Hi lucamar,

So I think I've got the right help file (the one that pops up when you press F1 over key word). I can find DBgrids, then TDBGrid and I can see under EventsByName a reference to OnDrawColumnTitle. However it is not in the Object Inspector so I can set a default routine for it that way. And if I tried to set up the event routine n code, the compiler doesn't find it as a member.

Also I'm not sure I'm looking at the right thing because if I follow the reference path down into columns and then into Items I can see in the help a function called CreateTitle, but again this doesn't appear as an option in the code when the editor window pops up after entering the '.'

There are other examples of this, so I'm assuming its me not reading things correctly, for example reference to TColumnTitle, which I can't find how to use.

Any help on my understanding of this would be hugely beneficial in my learning.

Many 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

wp

  • Hero Member
  • *****
  • Posts: 5740
Re: Title with filter box
« Reply #11 on: January 12, 2019, 01:22:53 pm »
I think you are expecting too much of the chm help files; I guess 95% of their content is autogenerated and of little use, and usually they are out-dated...

Please learn how to navigate in the Lazarus source code, this provides the most up-to-date documentation that you can imagine.

Let's assume, you have a TDBGrid on your form and you want to see the events available for it. Ctrl+click on the identifier "TDBGrid" your source code, and Lazarus opens the source file at the position where TDBGrid is implemented. Depending on the version you may be shown the implementation or interface part of the unit - press SHIFT+CTRL+ArrowUp/Down to switch between both sections. When you are in the interface part of TDBGrid in unit grids.pas, you can scroll down a bit and you see a list of all events.

If you have Laz 1.8.4 as suggested by your signature, however, you will not find OnDrawColumnTitle because this event is available in v2.0 or later only. You can try one of the release condidates to get it.

Continued usage of CTRL+Click will bring you anywhere. This is the standard method I am using when studying code.

Another important shortcut is CTRL+b which brings you back to the previously displayed source code position. Note that this is for classical keyboard layout. In the default layout, I think, the key combination is ALT+Left arrow.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: Title with filter box
« Reply #12 on: January 12, 2019, 01:56:08 pm »
Many thanks wp, will start down that route :)
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: 1610
Re: Title with filter box
« Reply #13 on: January 12, 2019, 03:22:57 pm »
I think you are expecting too much of the chm help files; I guess 95% of their content is autogenerated and of little use, and usually they are out-dated...
[...]
If you have Laz 1.8.4 as suggested by your signature, however, you will not find OnDrawColumnTitle because this event is available in v2.0 or later only.

You're right, of course; I just looked in the source and there's not any OnDrawColumnTitle in version 1.8.4. The mistery then is how it got to the docs ... back to the future?

@daveihull: My apologies for a bad advice; I was mislead by the docs and didn't take the time to check the code.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 5740
Re: Title with filter box
« Reply #14 on: January 12, 2019, 04:15:49 pm »
The mistery then is how it got to the docs ... back to the future?
Yes. Maybe the docs is for v2.0?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10