在Visual Studio用户控件中将字符串加倍 [英] String to double in Visual Studio usercontrol
问题描述
我有一个usercontrol
,其中只有一个文本框.我想将文本框的string
解析为double
(我正在进行大量计算).我认为在usercontrol的代码文件中执行此操作应该很容易,所以我尝试了如下操作:
I have a usercontrol
with only a textbox in it. I want to parse the string
of that textbox to a double
(I''m making lots of calculations). I thought it should be easy to do that in the code-file of the usercontrol, so I tried something like this:
public double txt
{
get { return double.Parse(txtInput.Text); }
set { txtInput.Text = value.ToString(); }
}
当我这样做时,没有问题.在我的form
上,我正在尝试做:
When I do this, there is no problem. On my form
I''m trying to do:
vCurrent = Math.Round(uscontrTextbox.txt * velocity, 4);
但是当我这样做时,出现以下错误:
Cannot implicitly convert type ''string'' to ''double''
此错误与以下格式的designer class
有关:
But when I do this I get the following error:
Cannot implicitly convert type ''string'' to ''double''
This error is related to the designer class
of the form:
this.uscontrTextbox.txt = "";
我该如何解决?
How can I solve this?
推荐答案
vCurrent = Math.Round(Convert.ToDouble(uscontrTextbox.txt) * velocity, 4);
或
而不是
or
instead of,
this.uscontrTextbox.txt = "";
使用,
this.uscontrTextbox.txt = 0;
祝您编程愉快!
:)
Happy coding!
:)
<pre>get
{
if(txtInput.Text=="")return 0;
else return double.Parse(txtInput.Text);
}
为了不将默认字符串设置为"0",我在用户控件中使用了以下代码:
In order to not set the default string as "0" I used the following code in my usercontrol:
public double txt
{
get { return double.Parse(txtInput.Text); }
set { txtInput.Text = value.ToString(); }
}
public string text
{
get { return txtInput.Text; }
set { txtInput.Text = value; }
}
然后在替换形式的设计器类中
And in the designer class of the form I replaced
this.uscontrTextbox.txt = "";
和
this.uscontrTextbox.text = "";
这篇关于在Visual Studio用户控件中将字符串加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!