C#从Windows窗体的文本框中定义变量

问题描述:

好吧,我是C#编程的新手,我遇到了这个问题,无法弄清楚如何从文本框中的用户输入定义变量,例如int,double.
请帮忙..
Net Framework 4.0

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

TextBox仅包含字符串.如果需要数字值,则必须强制转换或解析Text属性.我更喜欢解析:

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

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
 
}


您可以使用:
You can use this:
public int TextBoxValue
        {
            get
            {
                return int.Parse(TextBox.Text);
            }
            set
            {
                TextBox.Text = value.ToString();
            }
        }