blah blah blah is here! blah blah » Close

up0down
link

I am messing around trying to understand Classes and I am pretty sure I understand them now, but I am trying to make a Windows Form that has 2 text boxes where the user inserts a number in each one and then they get passed to a classes that adds the 2 together. I am taking it that textboxes are not considered INTs which makes sence, but how to I convert them to an INT?
thanks for any help.

last answered 3 months ago

1 answers

up0down
link

1st to get the string values in the textboxes
string text = textbox1.Text;
then to convert it to integer
int num = int.Parse(text);
but be sure that what you've type there are numbers because this may throw exception

up0down
link

is there a CHecktype? so I can do a check to make sure that textBox1.text is in fact a int and if not throw an error at the user calling them an idiot or something?? :D

up0down
link

put a try catch block
int i = 0;
try
{
i=int.Parse(txtMyText.Text);
}
catch(Exception)
{
MessageBox.Show("Please put only numbers");
}

up0down
link

Convert is most relaible way
do try this
int num1 = Convert.ToInt32(textBox1.Text);

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback