Search code examples
.netintegernumericupdown

how do i restrict numericupdown control to accept only integers


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


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.