5 Replies - 6528 Views - Last Post: 29 December 2010 - 10:03 PM Rate Topic: -----

#1 ClearSky22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-December 10

How to mute the Enter key press in NumericUpDown Box?

Posted 29 December 2010 - 03:32 PM

Hello...

When I press the Enter key in NumericUpDown box, it makes a beep sound.
Is there a way to mute it?

Also, when I enter a number in NumericUpDown1 box (without using up or down arrows),
it doesn't display it instantly in TextBox1, unless I press Enter.

How do I display the NumericUpDown value instantly in TextBox1,
without pressing Enter and hearing that beep sound?

Here is the code:

Public Class Form1
 Dim TB1 As Short

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  
 End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
  TB1 = NumericUpDown1.Value
  TextBox1.Text = TB1


Is This A Good Question/Topic? 0
  • +

Replies To: How to mute the Enter key press in NumericUpDown Box?

#2 Lumenii  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 04-January 07

Re: How to mute the Enter key press in NumericUpDown Box?

Posted 29 December 2010 - 07:54 PM

When dealing with key presses, keep these three events in mind:

    Private Sub NumericUpDown1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
        
    End Sub

    Private Sub NumericUpDown1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NumericUpDown1.KeyPress

    End Sub

    Private Sub NumericUpDown1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyUp

    End Sub

For getting rid of (or "muting") the sound on key press, you can use the following code in the NumericUpDown's KeyDown event.

    Private Sub NumericUpDown1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
        If e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True
    End Sub

You can use this to suppress that sound on other key presses as well, such as the spacebar.

    Private Sub NumericUpDown1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
        If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Space Then e.SuppressKeyPress = True
    End Sub

Let us know how that works out for you.

This post has been edited by Lumenii: 29 December 2010 - 07:55 PM

Was This Post Helpful? 1
  • +
  • -

#3 ClearSky22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-December 10

Re: How to mute the Enter key press in NumericUpDown Box?

Posted 29 December 2010 - 08:37 PM

Thank you, Lumenii, that worked perfectly! The beep sound has been removed.

I've now got only one problem left.
I've searched Google for the last two days, and couldn't find a solution for the following problem:

The NumericUpDown box does not cause the ValueChanged event to fire
if you enter some value using keyboard.

You need to press the Enter key.
Only then will the value be shown in other box(TetBox1.Text).

Here is the code:
Public Class Form1
Dim TB1 As Short

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
TB1 = NumericUpDown1.Value
TextBox1.Text = TB1
End Sub


How can I make the NumericUpDown value show instantly in other box?
Was This Post Helpful? 0
  • +
  • -

#4 Lumenii  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 04-January 07

Re: How to mute the Enter key press in NumericUpDown Box?

Posted 29 December 2010 - 09:10 PM

Well, as I'm sure you've noticed, the NumericUpDown unfortunately has no Text property or TextChanged event. However, it turns out that when the NumericUpDown control receives focus, it tries to parse/validate the text in its text field.

With that knowledge, we can "give" focus to the NumericUpDown after a key is pressed, using the KeyUp event, as so:

    Private Sub NumericUpDown1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyUp
        If IsNumeric(e.KeyValue) Then NumericUpDown1.Focus()
    End Sub

I decided to check for the pressed key having a numeric value using the KeyValue property, which is the keyboard value of the key that is pressed. If it is a numeric value (and therefore not a symbolic or alphabetic key), we 'trick' the NumericUpDown into checking for a new value.
Was This Post Helpful? 1
  • +
  • -

#5 ClearSky22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-December 10

Re: How to mute the Enter key press in NumericUpDown Box?

Posted 29 December 2010 - 09:40 PM

Wow, it works perfect.

Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 Lumenii  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 04-January 07

Re: How to mute the Enter key press in NumericUpDown Box?

Posted 29 December 2010 - 10:03 PM

View PostClearSky22, on 29 December 2010 - 08:40 PM, said:

Wow, it works perfect.

Thank you!


No problem. :) I was surprised I figured that last one out -- I hadn't known about the focus bit beforehand. So thanks to you as well. I'm sure I will have a use for it in the future.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1