Textbox only accepting numbers in C# windows application.
The following piece of code can be used in textbox keypress event to restrict user to enter only numbers.
private void txtZip_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}


Rojalin SahooPosted Feb 6, 2015, 1:19 AM EST
If you are using vs 2013 it is easy to use textmode property as 'Number' of the textbox.
ramesh jilkapallyPosted Jun 25, 2013, 11:38 PM EDT
no iam not keypres
waqas memonPosted Jul 25, 2012, 12:10 PM EDT
Hi follow this link it is for both for decimal and for integer http://codingresolved.com/discussion/41/how-to-restrict-textbox-to-numbertextbox-numbers-onlytextbox-just-numbers#Item_2
ahmed ahmedPosted Mar 4, 2010, 1:47 PM EST
please more explanation for how to use
venkatesh reddyPosted Mar 3, 2010, 1:00 AM EST
When i click the textbox , i am getting as private void textBox1_TextChanged(object sender, EventArgs e) { } But how u r getting like private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { }