按 Enter 时禁用 TSpinEdit 上的系统蜂鸣声

发布于 2024-09-07 03:18:07 字数 413 浏览 3 评论 0原文

我的窗体上有一个默认按钮,上面有 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 技术交流群。

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

发布评论

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

评论(3

场罚期间 2024-09-14 03:18:07

您必须从 TSpinEdit 下降并覆盖 IsValidChar 以避免 MessageBeep 调用,或覆盖 KeyPress 以避免 IsValidChar

You have to descend from TSpinEdit and override IsValidChar to avoid the MessageBeep call or KeyPress to avoid IsValidChar.

雨后彩虹 2024-09-14 03:18:07

试试这个

//Disable system beep
SystemParametersInfo(SPI_SETBEEP, 0, nil, SPIF_SENDWININICHANGE); 

//Enable system beep
SystemParametersInfo(SPI_SETBEEP, 1, nil, SPIF_SENDWININICHANGE); 

Try this one

//Disable system beep
SystemParametersInfo(SPI_SETBEEP, 0, nil, SPIF_SENDWININICHANGE); 

//Enable system beep
SystemParametersInfo(SPI_SETBEEP, 1, nil, SPIF_SENDWININICHANGE); 
海拔太高太耀眼 2024-09-14 03:18:07

在表单上设置 KeyPreview = True 并将以下代码添加到表单的按键事件中:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if SpinEdit1.Focused and (Key = #13) then
  begin
    Key := #0; // Cancels the keypress
    Perform(CM_DIALOGKEY, VK_RETURN, 0); // Invokes the default button
  end;
end;

Set KeyPreview = True on your form and add the following code to the form's keypress event:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if SpinEdit1.Focused and (Key = #13) then
  begin
    Key := #0; // Cancels the keypress
    Perform(CM_DIALOGKEY, VK_RETURN, 0); // Invokes the default button
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文