C# 将文本框转换为浮动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15078019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert textbox to float
提问by b-fg
I've been looking for different ways to do it, but I still get the same error:
我一直在寻找不同的方法来做到这一点,但我仍然遇到相同的错误:
What I've tried:
我试过的:
float e = (float)Convert.ToDouble(e_textBox.Text);
bool valid = float.TryParse(e_textBox.Text.ToString(), out e);
And I get this error:
我得到这个错误:
Error 1 Cannot implicitly convert type 'float' to 'System.EventArgs'
am I doing that wrong? Thank you.
我做错了吗?谢谢你。
采纳答案by Justin Niessner
I'm guessing your code lives inside an event handler. One of the parameters to your handle will be EventArgs e
:
我猜你的代码存在于一个事件处理程序中。句柄的参数之一是EventArgs e
:
public void OnClick(object sender, EventArgs e)
{
float e = (float)Convert.ToDouble(e_textBox.Text);
bool valid = float.TryParse(e_textBox.Text.ToString(), out e);
}
You just need to come up with a new variable name (or rename the parameter to something other than e):
您只需要想出一个新的变量名称(或将参数重命名为 e 以外的名称):
public void OnClick(object sender, EventArgs eargs)
{
float e = (float)Convert.ToDouble(e_textBox.Text);
bool valid = float.TryParse(e_textBox.Text.ToString(), out e);
}