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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 3, 2012, 5:15 AM EDT
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 Ki3 reputationPosted Jul 4, 2012, 2:33 AM EDT