how do i restrict numericupdown control to accept only integers

umbersar picture umbersar · May 13, 2011 · Viewed 7.7k times · Source

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

Answer

Emond Erno picture Emond Erno · May 13, 2011

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.