附加信息:输入字符串的格式不正确文本框 c#

Additional information: Input string was not in a correct format textbox c#

我的表单中有两个文本框 txtbox1 用于薪水,txtbox2 用于结果 (txtbox1 / 30)

我为 txtbox1 选择了自定义格式这是代码:

private void mtb_SJ02_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.Handled = !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != ',') // 8 is back space
    {
        if (e.KeyChar == (char)13) // 13 is Enter
        {
            mtb_SJ02.Text = string.Format("{0:#,##0.00}", double.Parse(mtb_SJ02.Text));
        }
    }
}

代码运行良好,如我所愿 显示如下数字:22.403,33 现在我需要分薪 (txtbox1.text/30)

我创建了一个名为 sj 的字符串变量:

  string  sj;

现在我想计算结果并以相同的格式 {0:#,##0.00} 将其显示在 txtbox2 中。这是代码:

void calculate ()
{
    sj = ( Double.Parse(mtb_SALAIR02.Text ) / 30).ToString("{0:#,##0.00}");
    mtb_SJ02.Text = sj;
}

当我 运行 代码时,我收到此消息错误: enter image description here

好主意,请问怎么做?

试试这个:

if(textBox1.Text=="")
{
  textBox1.Text="0";
}
textBox1.Text = string.Format("{0:#,##0.00}", double.Parse(textBox1.Text));
String sj = (Double.Parse(textBox1.Text, CultureInfo.CurrentCulture) / 30).ToString();
textBox2.Text = string.Format("{0:#,##0.00}", double.Parse(sj));

谢谢大家,我想我已经由朋友解决了这个问题

解决方案是:

        string  sj;

    void calculator ()
    {
        if (String.IsNullOrEmpty(mtb_SALAIR02.Text)) return;
        mtb_SALAIR02.Text = string.Format("{0:#,##0.00}", double.Parse(mtb_SALAIR02.Text));
        sj = (Double.Parse(mtb_SALAIR02.Text, CultureInfo.CurrentCulture) / 30).ToString();
        mtb_SJ02.Text = string.Format("{0:#,##0.00}", double.Parse(sj));
    }