Question

I have a windows numericupdown control. I want to restrict it so that user can only enter integers in it. How do I do that? At present a user can enter a decimal number as well. Thanks PS I am using .net

Was it helpful?

Solution

I did a little experimenting and found this workaround:

    private void numericUpDown1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar < 48 || e.KeyChar > 57)
        {
            e.Handled = true;
        }
    }

This way you will not be able to type thousand separators either but you could add that by first finding out what the thousand separator is and allowing that too.

OTHER TIPS

Set the DecimalPlaces property to zero.

If you are using the AjaxControlToolkit, you can just combine the FilteredTextBoxExtender with the NumericUpDownExtender:

<asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender" runat="server" TargetControlID="TextBoxNums" FilterType="Numbers">
</asp:FilteredTextBoxExtender>
<asp:NumericUpDownExtender ID="NumericUpDownExtender" runat="server" TargetControlID="TextBoxNums" Width="10">
</asp:NumericUpDownExtender>
<asp:TextBox ID="TextBoxNums" runat="server"></asp:TextBox>

If you have access to DevExpress controls, you should use a SpinEdit control and set its Properties.IsFloatValue to false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top