textbox text to int error

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I can't get a number out of my textbox. it keeps giving me an error and
saying that the input string was not in the correct format.

this.number = int.Parse((this.numberTextBox.Text.ToString());

How do I get the number out of the text box if I only entered small positive
integers?

dave
 
There looks to be too many brackets (parentheseseeses) in your code line
below, that would be my first place to check. Change that to:

this.number = int.Parse( this.numberTextBox.Text.ToString() );

then see if you still have the same problem. It might also be wise to check
that the text box has some text in before you try and convert it to an int.
So the below might be even better still:

if( this.numberTextBox.Text != string.Empty )
{
this.number = int.Parse( this.numberTextBox.Text );
}
 
Generally when I know a certain TextBox will only be able to take Int's I
pass it through a regex function to validate it and then I parse it from
string to int... I alos place a zero as the default text for the TextBox
this way (if you dont use regex) u can make sure that the field will never
be empty (unless changed, thats where regex comes in ;))
 
nevermind, I figured it out after several days :) It was just that I
disposed the textbox before I got the number out of it. so now I have the
dispose function at the end of that function instead of at the beginning
thanks anyway
dave
 
Dave said:
I can't get a number out of my textbox. it keeps
giving me an error and saying that the input string
was not in the correct format.

int iValue = 0;

try
{
iValue = Convert.ToInt32(txtValue.Text);
}
catch
{
// Deal with strings that aren't valid integers
}

P.
 
Please can you tell me which function converts integer to
string?You know,if I want to put a integer,lets say x,
variable to textbox1 what do I need to write?
 
djozy said:
Please can you tell me which function converts integer to
string?You know,if I want to put a integer,lets say x,
variable to textbox1 what do I need to write?
that's easy. textbox.text = int1.ToString();
just put a dot on the end of the variable and the function ToString() and it
will convert it.
dave
 
I know how it is, I just started on it a couple months ago and I found it so
hard that I gave up on doing my project for a little bit. but I'll give you
some advice to help you learn fast.
open up a notepad on the computer and copy and paste little snippets of
code and where they go in there so that you don't ever have to memorize
anything to use it. you will learn very fast this way. look up stuff on the
internet and paste new things there.
also, getting microsoft visual studio will help A TON. trust me. for
example. if you wrote the name of the integer you were talking about and
then put a dot infront of it, it would list the functions that you can use.
and ToString() is one of them. so you would have found that without having
to ask on here. it will help you find lots of stuff you otherwise wouldn't
know how to use or see.
well, good luck
dave
 
Back
Top