Delphi - OnKeyPress occurs before TStringGrid updates cell with new character

Posted by JMTyler on Stack Overflow See other posts from Stack Overflow or by JMTyler
Published on 2010-04-07T01:33:13Z Indexed on 2010/04/07 4:33 UTC
Read the original article Hit count: 433

Filed under:
|
|
|
|

Coding in Delphi, attaching an OnKeyPress event handler to a TStringGrid:

The OnKeyPress event fires before the grid cell that the user is typing into has actually updated its value with the key that has been pressed. This is obviously a problem, when I want to know what the contents of that cell are at this moment, as in, as the user modifies it.

The "hacked" solution is simple, if you're not considering every detail: just grab the value from the cell and, since the OnKeyPress event comes along with a Key parameter, append that value to the end - now you have the current value of the cell!

False. What if the user has selected all the text in the cell (ie: "foo") and they are now typing 'b'. Since they selected the text, it will be erased and replaced with the letter 'b'. However, the value of the cell will still display as "foo" in OnKeyPress, and the value of Key will be 'b', so the above logic would lead the application to conclude that the cell now contains "foob", which we know is not true.

So. Does anybody know how to get around this problem? Is there a way to make OnKeyPress react after the grid's contents have been updated, or perhaps a way to force an update at the start of the handler? I am desperately avoiding the use of the OnKeyUp event here, so any suggestions aside from that would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about tstringgrid