0

I am trying to set up a text box which allows you to enter a name and press enter to confirm that but i don't want windows to ding every time you press enter on it. How can i disable the sound in this case or all together.

I have already tried:

Private Sub EnterMapName_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    e.Handled = True
    e.SuppressKeyPress = True
    Select Case e.KeyCode
        Case Keys.Enter
            name = MapName.Text
    End Select
End Sub
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mithka
  • 9
  • 1
  • You can assign a button to `AcceptButton` property of the form and handle `Click` event of that button. Then when you press enter on any of the text box control, click event of the accept button will raise. – Reza Aghaei Jan 27 '19 at 15:03
  • Apart from the accept button, if you want to solve the problem of your current code, you need to move `e.SuppressKeyPress = True` into the case statement. Currently you are ignoring all key presses. It means your `TextBox` will not receive any key. – Reza Aghaei Jan 27 '19 at 15:03
  • Also, note that `e.SuppressKeyPress = True` also sets `e.Handled` to the same value. You just need the former. – Jimi Jan 27 '19 at 16:13
  • 1
    Why a `Select Case` when there is only one case? A simple If should do the trick. – Mary Jan 27 '19 at 17:34

1 Answers1

1

In your KeyPress event put this:

If e.KeyChar.GetHashCode = 851981 Then e.Handled = True 'TRAP THE ENTER KEY BEEP
video.baba
  • 817
  • 2
  • 6
  • 11
  • 4
    Isn't `If e.KeyChar = ChrW(Keys.Enter)` better than the magic number `851981`? – Reza Aghaei Jan 27 '19 at 15:09
  • Possibly... But never used the latter, only the former thus couldn't comment. – video.baba Jan 27 '19 at 15:22
  • I wouldn't recommend checking for a constant hash code as even only a tiny change in the internal .NET code could cause your code to break completely. Hash codes were meant as a unique identifier for objects at runtime (for instance the `Dictionary` and `HashTable` both use it extensively) and therefore aren't supposed to be constant over time. – Visual Vincent Jan 27 '19 at 17:18