Results 1 to 6 of 6

Thread: Either suppress sound or suppress the enter key press?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Either suppress sound or suppress the enter key press?

    I'm still relatively new to programming so bear with me. I'm writing a logic/probability game solver application and I'm nearly done; I'm just working on the ease of use, the interface, etc...

    On the game board, I have various NumericUpDown controls that hold values for different cards. When the user enters a value for a previously unknown card, I want them to be able to press enter (while the focus is on the NumericUpDown) and then have the program run the logical/probability tests. I want the user to be able to press enter here and have the same effect as if they had pressed the 'Calculate' button. I have that working, but, when enter is pressed, there is a beep system sound from the NumericUpDown and I don't want the beep sound going off every time the user presses enter.

    Here is the code as I had it before looking online:
    Code:
    Private Sub NumericUpDown1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
            Handles NumericUpDown1.KeyPress
            If e.KeyChar = Chr(13) Then 'Chr(13) = enter key
                CalculateButton_Click(Me, EventArgs.Empty) 'Run calculations
            End If
    End Sub
    I've looked into a number of methods online but I can't seem to suppress the key press or only mute the sounds from my application (I could mute ALL sounds... but that's no good).

    Thanks for any help!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Either suppress sound or suppress the enter key press?

    Add:-
    vbnet Code:
    1. e.Handled = True
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Either suppress sound or suppress the enter key press?

    Hmm, I really thought that would work when I went and just tried it, but it still beeps when I press the enter button. I modified the code to this:

    Code:
    Private Sub NumericUpDown1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles NumericUpDown1.KeyPress
        If e.KeyChar = Chr(13) Then e.Handled = True : CalculateButton_Click(Me, EventArgs.Empty)
    End Sub
    This probably doesn't mean anything, but when stepping through the code, the beep occurs immediately after then 'End Sub'. The focus is then set on the NumericUpDown after the beep.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Either suppress sound or suppress the enter key press?

    Enter is a tricky key to deal with in KeyPress. Although it does produce Chr(13) it is not the whole story. Always go for Key_Down when dealing with non-character keys if you have the choice.

    vb.net Code:
    1. Private Sub NumericUpDown1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
    2.         If e.KeyCode = Keys.Enter Then
    3.             CalculateButton_Click(Me, EventArgs.Empty)
    4.             e.SuppressKeyPress = True
    5.         End If
    6.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Either suppress sound or suppress the enter key press?

    It seems it doesn't work in with the NumericUpDown control. Sorry bout that. But what you can do is move all your code to KeyDown instead and set e.SuppressKeypress to True.

    [EDIT]

    Ah Dunfiddlin posted while I was writing....his code will work.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: Either suppress sound or suppress the enter key press?

    Thank you! Worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width