【问题标题】:Input string was not in correct format at textchangetextchange 处的输入字符串格式不正确
【发布时间】:2013-12-25 16:38:38
【问题描述】:

我已经写了这段代码

private void txtRate_TextChanged(object sender, EventArgs e)   
{
  try   
  {   
    decimal qty;   
    decimal price;   
    decimal amt = 0;   
    qty = Convert.ToDecimal(txtQuantity.Text);  
    price = Convert.ToDecimal(txtRate.Text);  
    amt = qty * price;  
    txtAmount.Text = amt.ToString();    
  }   
  catch (Exception ex)   
  {   
    MessageBox.Show(ex.Message);   
  }   
}

自动获取txtAmount文本框中的值,我可以正确获取。

之后我想清除所有文本框,所以我写了这个

txtProdSerName.Text = "";  
txtQuantity.Text = "";   
txtRate.Text = "";   
txtAmount.Text = "";  

但我遇到了错误

输入的字符串格式不正确

因为textchange事件在这里再次发生。

请给我一些建议或解决方案

【问题讨论】:

  • 你在哪一行得到这个错误? txtQuantity.TexttxtRate.Text 的值是多少?

标签: c# winforms textbox


【解决方案1】:

稍微修改一下代码,如下所示:

private void txtRate_TextChanged()
{
   try   
  {
    if(txtRate.Text.Trim() == string.Empty) return;
    decimal qty;   
    decimal price;   
    decimal amt = 0;   
    qty = Convert.ToDecimal(txtQuantity.Text);  
    price = Convert.ToDecimal(txtRate.Text);  
    amt = qty * price;  
    txtAmount.Text = amt.ToString();    
  }   
  catch (Exception ex)   
  {   
    MessageBox.Show(ex.Message);   
  }
}

这样,每次 txtRate 为空时,您都会退出 txtRate_TextChanged() 事件。

【讨论】:

  • 如果字符串为空,则从事件处理程序返回,非常清楚:)
  • :-) @SudhakarTillapudi :-)
【解决方案2】:

问题: 每次Textbox 值更改时都会触发TextChanged 事件。即使您清除了TextBox 值并导致异常,它也会被触发。

解决方案1:您可以在清除TextFeilds之前从TextChanged事件中unsubscribe在清除TextFeilds后再次使用subscribe,以便在清除TextBox值的同时它可以不要调用TextChanged 事件,因为TextChanged 没有subscribed 事件处理程序。

试试这个:

txtRate.TextChanged -= new System.EventHandler(txtRate_TextChanged);  

txtProdSerName.Text = "";  
txtQuantity.Text = "";   
txtRate.Text = "";   
txtAmount.Text = ""; 

txtRate.TextChanged += new System.EventHandler(txtRate_TextChanged);

解决方案2:您可以创建一个boolean 变量并检查它是否真的是一个TextChanged 事件。

第 1 步: 创建 Boolean 变量并将其初始化为 true
第 2 步:Boolean 变量值更改为 false在清除 Textbox 值之前。
第 3 步:在清除 Textbox 值后,将 Boolean 变量改回 true
第 4 步: 仅当Boolean 变量为true 时才执行TextChanged 事件处理程序代码。

试试这个:

private bool isTextChangedEvent = true;
private void txtRate_TextChanged(object sender, EventArgs e)   
{
  if(isTextChangedEvent)
  {
    try   
    {   
      decimal qty;   
      decimal price;   
      decimal amt = 0;   
      qty = Convert.ToDecimal(txtQuantity.Text);  
      price = Convert.ToDecimal(txtRate.Text);  
      amt = qty * price;  
      txtAmount.Text = amt.ToString();    
    }   
    catch (Exception ex)   
    {   
      MessageBox.Show(ex.Message);   
    }   
  }
}

在清除 TextBox 值时:

isTextChangedEvent = false;
txtProdSerName.Text = "";  
txtQuantity.Text = "";   
txtRate.Text = "";   
txtAmount.Text = ""; 
isTextChangedEvent = true;

【讨论】:

  • 附加和分离事件的成本很高。您还有其他优雅的解决方案吗?
  • @Aniket:我同意你的看法,让我用solution2 编辑我的答案。
  • @Aniket:用 Solution2 编辑了我的答案,我认为这应该对 OP 有所帮助。
  • 你正在添加一个额外的状态,这通常是一件坏事。保持程序无状态,你肯定会产生一些好的、防故障的代码。
  • @Aniket:对不起,我没听懂adding an extra state,它在第二个解决方案中吗?
【解决方案3】:

确保您的 txtQuantity.TexttxtRate.Text 不为空,因为当您清空文本框值时会触发 TextChangeEvent。

private void txtRate_TextChanged(object sender, EventArgs e)   
{
  try   
  {   
      decimal qty;   
      decimal price;   
      decimal amt = 0;
      if (!String.IsNullOrEmpty(txtQuantity.Text) && !String.IsNullOrEmpty(txtRate.Text))
      {
          qty = Convert.ToDecimal(txtQuantity.Text);  
          price = Convert.ToDecimal(txtRate.Text);  
          amt = qty * price;  
          txtAmount.Text = amt.ToString();    
      }     
  }   
  catch (Exception ex)   
  {   
      MessageBox.Show(ex.Message);   
  }   
}

【讨论】:

    【解决方案4】:

    问题

    当您更改文本时,它会触发事件,在此之前您正在设置 txtQuantity txtrate"",在这种情况下,您正在使用 txtQuantity txtrate 值(现在实际上是 Empty,因此它们无法解析为 decimal)这就是为什么当它尝试更改txtRate 的文本,它发现txtQuantity 值为""(空)并抛出异常。

    解决方案

    将此代码放在单独的方法中:

    private void changeTxtRateValue()
    {
        try   
          {   
            decimal qty;   
            decimal price;   
            decimal amt = 0;   
            qty = Convert.ToDecimal(txtQuantity.Text);  
            price = Convert.ToDecimal(txtRate.Text);  
            amt = qty * price;  
            txtAmount.Text = amt.ToString();    
          }   
          catch (Exception ex)   
          {   
            MessageBox.Show(ex.Message);   
          }
    }
    

    并且仅在txtQuantity 不是Empty 时调用该方法:

    private void txtRate_TextChanged(object sender, EventArgs e)   
    {
        if(txtQuantity.Text != String.Empty)
            changeTxtRateValue();
    }
    

    最佳实践建议:

    不要使用""设置字符串empty,因为它不是empty

    始终使用String.Empty

    使用此代码:

    txtProdSerName.Text = String.Empty;  
    txtQuantity.Text = String.Empty;   
    txtRate.Text = String.Empty;   
    txtAmount.Text = String.Empty;
    

    它会解决你的问题。

    【讨论】:

      【解决方案5】:

      您没有在应用程序中使用正确的控件。请改用 NumericUpDown 控件。

      【讨论】:

        猜你喜欢
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2013-08-22
        相关资源
        最近更新 更多