Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 238
umbersar Avatar asked May 13 '11 06:05

umbersar


2 Answers

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.

like image 117
Emond Avatar answered Oct 08 '22 11:10

Emond


Set the DecimalPlaces property to zero.

like image 20
Jamie Kitson Avatar answered Oct 08 '22 11:10

Jamie Kitson