)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to restrict the input mode in the WinForms NumericUpDown (NumericUpDownExt) by using only the up or down button?

Platform: WinForms |
Control: NumericUpDownExt |
Published Date: August 4, 2015 |
Last Revised Date: January 7, 2020

Restrict the input mode

You can restrict the input mode of the NumericUpDownExt by using the up/down buttons alone. This requirement can be achieved by the following steps.

  1. By hiding the caret (cursor point) in the NumericUpDownExt.
  2. By restricting value input via the KeyPress and MouseWheel.

 

Hide the caret:

The function, HideCaret helps to remove the caret (cursor point) from the NumericUpDownExt. The User32.dll should be imported to get the HideCaret function.

C#

[DllImport("user32")]
private static extern bool HideCaret(IntPtr hWnd);
void numericUpDownExt1_GotFocus(object sender, EventArgs e)
{
    //Hides the caret from the NumericUpDownExt.
    HideCaret(this.numericUpDownExt1.Controls[1].Handle);
}

VB

<DllImport("user32")> _
Private Shared Function HideCaret(ByVal hWnd As IntPtr) As Boolean
Private Sub numericUpDownExt1_GotFocus(ByVal sender As Object, ByVal e As EventArgs)
    'Hides the caret from the NumericUpDownExt.
    HideCaret(Me.numericUpDownExt1.Controls(1).Handle)
End Sub

 

Note:

Hiding a caret does not destroy the current shape or invalidate the insertion point.

 

Restrict the KeyPress and MouseWheel input:

The value of the NumericUpDownExt can be changed via Mouse wheel, key press, and the Up/Down buttons, by default. As this requirement is to restrict the value update, other than the up/down buttons, it can be achieved by handling the Mouse wheel and Key press events in the NumericUpDownExt.

C#

void numericUpDownExt1_KeyPress(object sender, KeyPressEventArgs e)
{
    //Avoids the KeyPress event in the NumericUpDownExt.
    e.Handled = true;
}
void numericUpDownExt1_MouseWheel(object sender, MouseEventArgs e)
{
    //Passes the Mousewheel event to the parent container.
    ((HandledMouseEventArgs)e).Handled = true;
}

VB

Private Sub numericUpDownExt1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    'Avoids the KeyPress event in the NumericUpDownExt.
    e.Handled = True
End Sub
Private Sub numericUpDownExt1_MouseWheel(ByVal sender As Object, ByVal e As MouseEventArgs)
    'Passes the Mousewheel event to the parent container.
    CType(e, HandledMouseEventArgs).Handled = True
End Sub

 

Caret is hidden in the NumericUpDownExt

Figure 1: Caret is hidden in the NumericUpDownExt

Samples:

C#: http://www.syncfusion.com/downloads/support/directtrac/141608/ze/NumericUpDownExt2123534338

VB: http://www.syncfusion.com/downloads/support/directtrac/141608/ze/NumericUpDownExt_VB-2004341231

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Carlos Javier Flores Kanter
Nov 19, 2015
How can I do the samething but with JS? Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon