|
-
May 13th, 2013, 07:01 PM
#1
Thread Starter
New Member
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!
-
May 13th, 2013, 07:05 PM
#2
Re: Either suppress sound or suppress the enter key press?
-
May 13th, 2013, 07:20 PM
#3
Thread Starter
New Member
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.
-
May 13th, 2013, 07:25 PM
#4
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:
Private Sub NumericUpDown1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
If e.KeyCode = Keys.Enter Then
CalculateButton_Click(Me, EventArgs.Empty)
e.SuppressKeyPress = True
End If
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!
-
May 13th, 2013, 07:27 PM
#5
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.
-
May 13th, 2013, 07:31 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|