Delphi - OnKeyPress 在 TStringGrid 用新字符更新单元格之前发生

发布于 2024-08-28 02:36:26 字数 567 浏览 8 评论 0原文

在 Delphi 中进行编码,将 OnKeyPress 事件处理程序附加到 TStringGrid:

OnKeyPress 事件在用户正在键入的网格单元实际使用按下的键更新其值之前触发。当我想知道此时该单元格的内容是什么时,这显然是一个问题,就像用户修改它一样。

如果您不考虑每个细节,“黑客”解决方案很简单:只需从单元格中获取值,并且由于 OnKeyPress 事件附带一个 Key 参数,因此将该值附加到末尾- 现在你就得到了单元格的当前值!

错误的。如果用户选择了单元格中的所有文本(即:“foo”)并且现在输入“b”,该怎么办?由于他们选择了文本,因此它将被删除并替换为字母“b”。但是,单元格的值在 OnKeyPress 中仍将显示为“foo”,并且 Key 的值将为“b”,因此上述逻辑将导致应用程序得出结论,单元格现在包含“foob”,我们知道这不是真的。

所以。有人知道如何解决这个问题吗?有没有办法让 OnKeyPress 在网格内容更新后做出反应,或者有办法在处理程序开始时强制更新?我极力避免在这里使用 OnKeyUp 事件,因此除此之外的任何建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-09-04 02:36:26

如果您希望响应单元格中的更改,为什么不使用OnSetEditText事件?

键盘事件用于响应键盘输入,而不是对该输入的控件响应。控件通常会提供一个或多个附加事件,以允许应用程序响应控件对某些输入的响应 - 例如,在这种情况下,键盘输入会导致单元格值被修改,而不是单元格选择的更改。

顺便说一句,这些事件应该在网格控件响应这些事件之前发生,这是很自然的,这样您就可以在这些事件发生之前有效地“过滤”此类事件到达控件,例如防止特定的键盘输入影响单元格的值。

If you wish to respond to a change in a cells value why are you not using the OnSetEditText event ?

Keyboard events are for responding to keyboard input, not a controls response to that input. A control will typically provide one or more additional events to allow an application to respond to the controls response to some input - in this case, where a keyboard input results in a cell value being modified, rather than a change in cell selection, for example.

As an aside, it is quite natural that these events should be occuring prior to the grid controls response to those events, so that you may effectively "filter" such events before they reach the control, e.g. to prevent a particular keyboard input from affecting the value of a cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文