且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-06 12:03:10

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();
            }
        }