按 Enter 时禁用 TSpinEdit 上的系统蜂鸣声
我的窗体上有一个默认按钮,上面有 TSpinEdit 控件。当 TSpinEdit 控件获得焦点并且用户按下 Enter 键时,用户不会听到默认按钮,而是会听到系统蜂鸣声,因为 Enter 键对于 TSpinEdit 无效。
通常,为了避免发出蜂鸣声,我会使用 OnKeyPress 事件并设置 Key := 0
来跳过按键。然后我可以在默认按钮上执行 click 方法。但是,在这种情况下,OnKeyPress 不会触发,因为 Enter 键无效。
OnKeyDown 触发,但是当我在那里设置 Key := 0
时,它不会停止系统蜂鸣声。
那么,如何在 TSpinEdit 控件上按 Enter 键时禁用系统蜂鸣声?
我使用的是 Delphi 5,他们没有包含 Spin.pas 的源代码。
I have a default button on a form that has a TSpinEdit control on it. When the TSpinEdit control has the focus and the user presses the Enter key, instead of the default button getting clicked, the user just hears a system beep because the Enter key is invalid for a TSpinEdit.
Normally, to avoid the beep, I would use the OnKeyPress event and set the Key := 0
to skip the key press. I could then execute the click method on the default button. However, in this case, OnKeyPress doesn't fire because the Enter key is not valid.
OnKeyDown fires, but when I set Key := 0
there, it doesn't stop the system beep.
So, how do I disable the system beep when pressing the Enter key on a TSpinEdit control?
I'm on Delphi 5, and they didn't include the source for Spin.pas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须从
TSpinEdit
下降并覆盖IsValidChar
以避免 MessageBeep 调用,或覆盖KeyPress
以避免IsValidChar
。You have to descend from
TSpinEdit
and overrideIsValidChar
to avoid the MessageBeep call orKeyPress
to avoidIsValidChar
.试试这个
Try this one
在表单上设置 KeyPreview = True 并将以下代码添加到表单的按键事件中:
Set KeyPreview = True on your form and add the following code to the form's keypress event: