Articles in this section
Category / Section

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

2 mins read

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#: https://www.syncfusion.com/downloads/support/directtrac/141608/ze/NumericUpDownExt2123534338

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied