Hi,
I'm a newbie in visual C# and i need to read values from a textbox into an integer variable.
The user is not allowed to input other characters but numbers into the textbox.
int a1;
string ia1;
ia1 = txtMNA1.Text.ToString();
a1 = Convert.ToInt32(ia1);
This is what i wrote and it returns "Input string was not in a correct format." runtime error.
Thanks for your help,
Hristo
Loading
Jan MontanoPosted Jan 12, 2009, 10:11 PM
Aleksandar HristovskiPosted Jan 12, 2009, 9:54 PM
Aleksandar HristovskiPosted Jan 12, 2009, 8:19 AM
so i set a message box to display the string variable ia1 and it returns void, so somehow the contents of the textbox never get copied into the string variable.
i double checked everything and i can't find a problem, so i guess it's a computer specific bug, if you have other suggestions please let me know.
Jan MontanoPosted Jan 12, 2009, 7:02 AM
string ia1;
ia1 = textBox1.Text;
int result;
if (Int32.TryParse(ia1, out result))
{
a1 = Convert.ToInt32(ia1);
MessageBox.Show(string.Format("Input {0} is an integer", ia1));
}
else
{
MessageBox.Show(string.Format("Input {0} is not an integer", ia1));
}
Aleksandar HristovskiPosted Jan 12, 2009, 3:24 AM
I'm thinking of taking the ascii value of the string, subtract 48 out of it and setting the integer to be the result.
However i can't get the ascii of a string, is there function for it?
Thanks,
Hristo
Scott LyslePosted Jan 11, 2009, 11:53 PM
Blocked AccountPosted Jan 11, 2009, 11:52 PM
Code u have written is working in my end for integer values. because u are converting string value to integer, so u have to take care about the input values.
That error will come if u provide the values like that 123c,12.00 and these values can't be converted into int because now these are string values. So put the validation in the textbox for accepting the integer values.