0

I know the question has been asked and answered many times, but what if none of the answers work? I'm using Visual Studio 2017 to rewrite a vb6 app that catches the key combination "ctrl+enter" to load a dialogue. I catch the key combination just fine from a textbox called CourtName, but can't get rid of the annoying "ding" that goes with it. I have googled for many hours but everywhere the answer is to use e.Handled and/or e.SuppressKeyPress, which I have done without success. Here is my code:

Private Sub CourtName_KeyDown(sender As Object, e As KeyEventArgs) Handles CourtName.KeyDown
        If e.KeyCode = Keys.Enter AndAlso e.Control Then
            e.Handled = True
            e.SuppressKeyPress = True
            CourtsBtn.PerformClick()
        End If
End Sub

The ding still persists, no matter whether the e.Handled and e.SuppressKeyPress statements are before or after the PerformClick() statement. What magic am I missing?

Ray E
  • 134
  • 1
  • 9
  • Does this answer your question? [Stop the 'Ding' when pressing Enter](https://stackoverflow.com/questions/6290967/stop-the-ding-when-pressing-enter) – esqew Apr 25 '20 at 04:38
  • Nope. Like I said, the answers are already in my code but the ding persists. This is really frustrating. – Ray E Apr 25 '20 at 04:52
  • Apologies - you haven't mentioned anything about utilizing the `Form.AcceptButton` methodology from the linked question's accepted answer, nor is it included in the snippet you've posted. – esqew Apr 25 '20 at 04:53
  • I don't want such a button so it's not applicable. Anyway, even if I set a Form.AcceptButton the ding still persists. – Ray E Apr 25 '20 at 05:01
  • 1
    For the record, your code will catch *Ctrl+Enter*, *Ctrl+Shift+Enter*, *Ctrl+Alt+Enter* and *Ctrl+Shift+Alt+Enter*. To catch just *Ctrl+Enter* you would need to use `e.KeyCode = Keys.Enter AndAlso e.Control AndAlso Not e.Shift AndAlso Not e.Alt`. This is why you should use `KeyData` rather than `KeyCode` for specific key combinations: `e.KeyData = (Keys.Control Or Keys.Enter)`. That's a much more succinct way to catch *Ctrl+Enter* specifically. – jmcilhinney Apr 25 '20 at 05:19
  • Nice tip. Thanks for that. – Ray E Apr 25 '20 at 05:32

2 Answers2

0

Try replacing the following code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Multiline = True
    MsgBox("Pressed!")
End Sub

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyData = Keys.Control + Keys.Enter Then
        TextBox1.Multiline = False
        Button1.PerformClick()
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Multiline = True
End Sub

I've tested this code on mine, it works perfect!

100% working code

Enjoy it!

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • @RayE This updated code works 100% well, I've tried. NO DINGS! Yay! – Rohan Bari Apr 25 '20 at 06:35
  • Thanks bud. Tried that too. It worked the first time I pressed Ctrl+Enter in the textbox and I thought problem solved, but alas, pressing Ctrl+Enter again without closing the form makes the ding again. I believe this may be a VB.Net or system problem seeing that some get it and others not. Best solution so far, disable sound. :) – Ray E Apr 25 '20 at 06:48
  • But no dings even after `Control + Enter` time and again in mine. Try to create a new project and run the code again, then you'll get to know. – Rohan Bari Apr 25 '20 at 06:52
  • Ok, I got it working for multiple time too!! Your multiline trick works. The textbox must be Multiline = True when Ctrl+Enter is pressed. So, what I did was set Multiline = True in Form load as you suggested, then set Multiline = False in the Keydown handler as you suggested. However the textbox Multiline = True must be set again for it to work again, so I did this in the Button click event. Now no more dings! I'm not sure if this is the right solution, but it works. Thank you so much. – Ray E Apr 25 '20 at 07:01
  • Glad to help you! And remember, there's no any other option to suppress `Control + Enter` keys, rather it's achievable for only `Enter` key by using form's `AcceptButton` property. – Rohan Bari Apr 25 '20 at 07:04
0

I just tested this code and there was no ding, no matter what modifiers were used or not with Enter:

Public Class Form1

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            'Suppress Windows audio feedback.
            e.Handled = True
            e.SuppressKeyPress = True
        End If

        If e.KeyData = (Keys.Control Or Keys.Enter) Then
            Button1.PerformClick()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Console.WriteLine("Button1_Click")
    End Sub

End Class

Can you test the same code and see whether it works for you?

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Tested, but ding persists. I see your logic though. Now we need to find out why you don't get a ding and I do - same code. By the way, when I click the button called by PerformClick() no ding occurs, so no other code is causing the ding. – Ray E Apr 25 '20 at 05:45
  • If you create a brand new project and try the code above, do you still get a ding? Good to check whether it's the one project or your system in general. – jmcilhinney Apr 25 '20 at 05:59
  • Yes sir, I tested with a brand new project. One textbox and one button, but the ding still persists - so it's not the code then. It seems it's something in my system? I'd love to know what! – Ray E Apr 25 '20 at 06:10