in Personal

C# Integer Only Textbox Example

I added a little example program the other day and I just wanted to let people know it was there in case it helps someone. It’s called “Integer Only Textbox Example” and it’s written in C# (what else is there? 😉 ). It will show you how to use the keypress event of a standard WinForm TextBox to force it to accept only positive and negative integers. I wrote this as an example to help a fellow blogger on here, but hopefully it will help other people out a little bit too. Also, you should make sure you actually double check that the value in the textbox WAS an integer (e.g. don’t just assume this works 100% of the time, because although it should, you should still use try/catch around Int32.Parse(textBox.Text) just to be safe). Anyway, here it is: