4

I made an edit control able to accept data when somebody would click "Enter". I used subclassing to do that. It worked almost perfectly. However after clicking "Enter" the system plays "Error" sound every time. I tried to use ES_MULTILINE and ES_AUTOVSCROLL to bypass it but it helped partially. Now after clicking "Enter" there's no sound, but in the textbox appears useless "Enter" character, that is impossible to delete. How to bypass the system sound? Or delete "Enter" character from that textbox (SetWindowText(handle, "") doesn't help).

user3366592
  • 439
  • 7
  • 23
  • 2
    Did you try `ES_WANTRETURN`? – nwp Apr 25 '14 at 14:53
  • 2
    It beeps because it doesn't make sense to press Enter in an edit control that's not multi-line. Enter is reserved to operate the default control of a window, like the OK button. Why are you pressing useless keys? What is supposed to happen instead? – Hans Passant Apr 25 '14 at 15:56
  • @nwp - ES_WANTREURN didn't change anything. – user3366592 Apr 25 '14 at 16:38
  • @Hans Passant - What do you mean by pressing useless keys? I want to make convenient way to confirm data instead of clicking a button using mouse. – user3366592 Apr 25 '14 at 16:42
  • No real idea what "confirm data" might mean. Sounds to me you are trying to write a console mode program. That's not how a GUI program is supposed to be designed. The Enter key operates the default control, the user presses the Tab key or the cursor keys to change the focus. – Hans Passant Apr 25 '14 at 17:32
  • 2
    @HansPassant: I've had the request very often from customers that pressing Enter should focus the next edit field. Especially if the program was to replace an old text mode data entry application. The users don't care about Windows UI guidelines, they want the key sequences to stay the same, muscle memory and so on. – mghie Apr 25 '14 at 17:44

2 Answers2

5

You do not need the ES_MULTILINE, ES_AUTOVSCROLL or ES_WANTRETURN style flags.

To stop a single-line edit control from beeping on VK_RETURN you need to handle the WM_CHAR message for that control and return 0 for VK_RETURN without calling the default window procedure, which has to be called for all other keys.

mghie
  • 32,028
  • 6
  • 87
  • 129
  • 2
    You do not need to handle the `WM_KEYDOWN` message. If you're not handling `WM_KEYDOWN` default processing (through a call to `TranslateMessage`) will produce one or more `WM_CHAR` messages. Handling `WM_CHAR` is sufficient to prevent the beep. – IInspectable Apr 25 '14 at 16:39
  • See [How to turn off beeping when pressing ENTER on a single-line EDIT control under Windows CE?](https://stackoverflow.com/a/3590126/1377770) for a code example. – Frenzy Li Dec 29 '17 at 04:31
3

I learned from WinAPI Reference that default processing of WM_CHAR for an edit calls MessageBeep function for an illegal character, such as enter and tab. I had subclassed the edit control to tab between controls by intercepting WM_KEYDOWN (as shown by Petzold for scroll bars), but it was beeping when I pressed tab. So I intercept WM_CHAR to avoid default processing when I press tab, and so stop the beep.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43