2017-03-07 95 views
2

我试图创建一个计算总量并将其显示在文本框中的公式,但出现如下错误: “输入字符串未正确显示格式。”公式中输入的字符串格式不正确

private void txtQUANTITYPOS_TextChanged(object sender, EventArgs e) 
{ 
    txtTOTALPOS.Text = (Convert.ToDouble(txtPricePOS.Text) * Convert.ToDouble(txtQUANTITYPOS.Text)).ToString(); 
} 

你能帮助我,请提前再次感谢您:)

+3

你有GOOGLE上搜索您的问题或应我们为你做? –

+3

某些文本框似乎不是'双'格式 –

+1

可能重复的[输入字符串不是以正确的格式C#](http://stackoverflow.com/questions/9286319/input-string-was-not-在一个正确的格式的C - 锐) – crashmstr

回答

3

一种可能的情况 - 你可能在你的txtQUANTITYPOS文本框中输入一个数字,但txtPricePOS文本框仍然是空的。因此,您可能会收到此错误,因为您的txtPricePOS文本框尚未包含数字。

你可以使用Double.TryParse代替,以确保您不会乘其它字符而不是数字:

继你如何执行它的一个例子:

private void txtQUANTITYPOS_TextChanged(object sender, EventArgs e) 
    { 
     double pricePos; 
     double qtyPos; 

     if (!double.TryParse(txtPricePOS.Text, out pricePos)) 
      return; // Will return in case txtPricePOS.Text is not a number 

     if (!double.TryParse(txtQUANTITYPOS.Text, out qtyPos)) 
      return; // Will return in case txtPricePOS.Text is not a number 

     txtTOTALPOS.Text = (pricePos * qtyPos).ToString(); 

    } 
+0

要去试试这个mam/sir,谢谢 – Pudge

+0

OMYYYYYYYYYY GHAD – Pudge

+0

它的工作感谢您!!!!!!!!! !!!!!!!!!!!!!!!!!!!非常!!!!!!!!!!!!!!!!! – Pudge