I am new at programming and I cant figure out how to read a integer value from my keyboard at a WindowsFormsApplication in c#.
I tried using a textBox or a maskedtextBoxed but it didnt work out.
Any thoughts?
Thanks a lot!
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 3, 2012, 9:15 AM
int i = int.Parse(textBox1.Text);
It's also possible to make sure that what's in the textbox is a valid integer and, if it isn't, to ask the user to re-input it:
int i;
bool isValid = int.TryParse(textBox1.Text, out i);
if (!isValid)
{
MessageBox.Show("Not a valid integer. Please re-enter");
textBox1.Focus();
return;
}
// otherwise 'i' contains the integer value of what's in the textbox
Zimi KiPosted Jul 4, 2012, 6:33 AM