text文本框为空或者不是数字时候如何在计算时候不计算,就像excel那样。
比如我有十个文本框要求和,有些没有输入,或者输入的不是数值,则不参与计算,有什么方法吗?
我现在搞得头都痛了。高手赐教,即使给个方向也好。

解决方案 »

  1.   

    比如我有十个文本框要求和,有些没有输入,或者输入的不是数值,则不参与计算,有什么方法吗? 
    ==
    这十个文本框要在一个容器里比较好操作
    否则大致如下int temp = 0;
    //判断是否为空
    if (!string.IsNullOrEmpty(TextBox1.Text.Trim()))
    {
        //判断是否能转换
        if (int.TryParse(TextBox1.Text, out temp))
        { 
            
        }
    }
      

  2.   

    将文本框都放到一个GroupBox中,
    在按钮事件中写: int sum = 0;
    foreach(Control childControl in this.groupBox1.Controls)
    {
    int count = 0;
    string str = string.Empty;
    if(childControl is TextBox)
    {
    str = childControl.Text.Trim();
    if(str.Length != 0)
    {
    char[] arrChar = str.ToCharArray(0, str.Length);
    foreach(char char1 in arrChar)
    {
    if(!char.IsDigit(char1))
    {
    count++;
    break;
    }
    }
    if(count != 0)
    continue;
    else
    sum += int.Parse(str);
    }
    }
    } this.label1.Text = sum.ToString();
      

  3.   

    我的情况是这样,有3行10列共有30个textbox,在前2行输入数字后,点击计算,则最后一行出现相应的值。
    在有些情况下,某些列不用输入值进行计算,我把他们留空或者打入“/”等符号,在点击计算时候将前2行的空值
    或者输入非数值时候时候,这些textbox不参加相应的计算,当作0来处理。
      

  4.   

    我用的是vs2005 ,bs网页模式,没有找到groupbox,倒是有panel,估计也可以用咯。
      

  5.   

     str = childControl.Text.Trim();
    没有啊
      

  6.   

    这里的childControl是
    foreach(Control childControl in this.Panel1.Controls)
    里的一个变量