Disable the system beep on TSpinEdit when you press Enter

I have a default button in a form that has a TSpinEdit control on it. When the TSpinEdit control has focus and the user presses the Enter key, instead of pressing the default button, the user simply hears a system beep because the enter key is not valid for TSpinEdit.

Usually, to avoid a beep, I used the OnKeyPress event and set Key := 0 to skip a keystroke. Then I could execute the default click method. However, in this case, OnKeyPress does not start, because the Enter key is invalid.

OnKeyDown is triggered, but when I set Key := 0 there, it does not stop the system beep.

So, how do I turn off the system beep when I press the Enter key in the TSpinEdit control?

I am on Delphi 5 and they did not include the source for Spin.pas.

+6
delphi
source share
3 answers

You need to go down from TSpinEdit and override IsValidChar to avoid calling MessageBeep or KeyPress to avoid IsValidChar .

+6
source share

Try this one

 //Disable system beep SystemParametersInfo(SPI_SETBEEP, 0, nil, SPIF_SENDWININICHANGE); //Enable system beep SystemParametersInfo(SPI_SETBEEP, 1, nil, SPIF_SENDWININICHANGE); 
+7
source share

Set KeyPreview = True in your form and add the following code to the form key press 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; 
+2
source share

All Articles