Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Well, I'm kinda new to C# programming, and I ran into this problem and I can't figure out how to define a variable such as an int, double, from user input in a textbox.
Help Please..
Net Framework 4.0
Posted
Updated 9-Aug-11 6:00am
v2
Comments
Chris Maunder 9-Aug-11 12:15pm    
We can't answer if you haven't told us what you've tried and where your error is. Closing the question.
Chris Maunder 9-Aug-11 12:15pm    
We can't answer if you haven't told us what you've tried and where your error is. Closing the question.
Chris Maunder 9-Aug-11 12:15pm    
We can't answer if you haven't told us what you've tried and where your error is. Closing the question.

A TextBox contains only strings. You have to cast or parse the Text property if you want a numeric value. I prefer parsing:

C#
int x;
if (int32.TryParse(textBox1.Text, out x))
{
    // you have a valid integer
    // and you can do what you want with x
}
else
{
    // the text contains invalid characters
    // put up an error message
 
}
 
Share this answer
 
v2
You can use this:
C#
public int TextBoxValue
        {
            get
            {
                return int.Parse(TextBox.Text);
            }
            set
            {
                TextBox.Text = value.ToString();
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900