C#从Windows窗体的文本框中定义变量 [英] C# Define Variable from Text Box in Windows Forms

查看:544
本文介绍了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();
            }
        }


这篇关于C#从Windows窗体的文本框中定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆