Recent

Author Topic: Catching a KeyPress in a StringGrid-Cell  (Read 3601 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3003
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #15 on: March 14, 2022, 10:01:53 pm »
Sooo…….. Bug or feature?
:)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 3003
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #16 on: March 15, 2022, 08:35:03 am »
If goAlwaysShowEditor=False it still misses the first KeyPress, but fires on the second ff.
Cannot confirm. Fires correctly
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #17 on: March 15, 2022, 01:09:20 pm »
Had a closer look at the sources today. Column ReadOnly issue left aside for the moment, it indeed fires correctly if the editor is already shown, either by setting goAlwaysShowEditor or by clicking the selected cell twice before entering the first key. Otherwise it will only fire at the second key. Reason is that in this case the editor is invoked by the grid's KeyPress handler by calling a method called EditorShowChar, which does not trigger it's own KeyPress and thus not your custom handler.

Next thing is that the editor's OnKeyPress is already hooked to a handler inside the grid called EditorKeyPress(). In this method there's a comment reading

Code: Pascal  [Select][+][-]
  1. // grid must get all keypresses, even if they are from the editor

followed by calling the grid's handler. So it seems to be a bad idea to just replace this method by your custom handler. You can, however, call this method from within your custom handler:

Code: Pascal  [Select][+][-]
  1. StringGrid1.EditorKeyPress(Sender, Key);

If you don't the grid will possibly not work as intended with key strokes like Esc and with a TDBGrid it won't check anymore if the key is accepted by the underlying field.

Given all this it might indeed not be a good idea to interfere with this rather complex arrangement but to use the grid's exposed OnKeyPress instead.
« Last Edit: March 15, 2022, 01:11:24 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3003
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #18 on: March 15, 2022, 02:48:35 pm »
hmmm..... you're right.
OK, if implementing a custom OnCellKeyPress for the Editor, never forget to call the Grids EditorKeyPress. Got it.

Albeit, the "First-Column-ReadOnly"-Issue forces me to do everything in the Grids OnKeyPress currently, since for the Editor itself it doesn't fire
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018