写了个求平均分的简单代码 在文本框里输入空值或则字母 会弹错误  代码里很简单的申明了一下 怎么样才能输入不符合值的时候不报错和不进行运算呢 我的代码是
 float a, b, c, d;
            a = float.Parse(textBox1.Text);
            b = float.Parse(textBox2.Text);
            c = float.Parse(textBox3.Text);
            d = (a + b + c)/3;
            textBox4.Text = d.ToString();
            MessageBox.Show(textBox4.Text,"平均分");
有人能帮我解说下吗 举下例子更好 谢谢!

解决方案 »

  1.   

    太简单了
    try
    {
    float a, b, c, d; 
    a = float.Parse(textBox1.Text); 
    b = float.Parse(textBox2.Text); 
    c = float.Parse(textBox3.Text); 
    d = (a + b + c)/3;
    }
    catch
    {
    return;
    } textBox4.Text = d.ToString(); 
    MessageBox.Show(textBox4.Text,"平均分");
      

  2.   

    太简单了
    try
    {
    float a, b, c, d; 
    a = float.Parse(textBox1.Text); 
    b = float.Parse(textBox2.Text); 
    c = float.Parse(textBox3.Text); 
    d = (a + b + c)/3;
    }
    catch
    {
    return;
    } textBox4.Text = d.ToString(); 
    MessageBox.Show(textBox4.Text,"平均分");
      

  3.   

    给你方法:
    #region  设置文本框只能输入数字型字符串
            /// <summary>
            /// 文本框只能输入数字型和单精度型的字符串.
            /// </summary>
            /// <param name="e">KeyPressEventArgs类</param>
            /// <param name="s">文本框的字符串</param>
            /// <param name="n">标识,判断是数字型还是单精度型</param>
            public void Estimate_Key(KeyPressEventArgs e,string s,int n)
            {
                if (n==0)   //只能输入整型
                    if (!(e.KeyChar <= '9' && e.KeyChar >= '0') && e.KeyChar != '\r' && e.KeyChar != '\b')
                    {
                        e.Handled = true;   //处理KeyPress事件
                    }
                if (n == 1) //可以输入整型或单精度型
                {
                    if ((!(e.KeyChar <= '9' && e.KeyChar >= '0')) && e.KeyChar != '.' && e.KeyChar != '\r' && e.KeyChar != '\b')
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        if (e.KeyChar == '.')   //如果输入“.”
                            if (s == "")    //当前文本框为空
                                e.Handled = true;   //处理KeyPress事件
                            else
                            {
                                if (s.Length > 0)   //当文本框不为空时
                                {
                                    if (s.IndexOf(".") > -1)    //查找是否已输入过“.”
                                        e.Handled = true;   //处理KeyPress事件
                                }
                            }
                    }
                }
            }
            #endregion用法:放在你的textbox的keypress事件里面:
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                Estimate_Key(e,textBox1.Text,1);//1表示可以输入整数和浮点数,0表示只能输入整数
                
            }
      

  4.   

            
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !Char.IsDigit(e.KeyChar); // 只能输入数字
    }
      

  5.   

    To 4楼:
    这样的小数点就不能输入了。这样不太好。
    楼主定义float a, b, c, d; 肯定要输入小数的。
      

  6.   

    再放开一些想放到键不就可以了
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = !Char.IsDigit(e.KeyChar); // 只能输入数字
                if (e.KeyChar == '.' && textBox1.Text.IndexOf('.') ==-1) // 再加上可以输入一个小数点
                    e.Handled = false;
                if (e.KeyChar == '-' && textBox1.Text.Length==0) // 再加上可以再最前面加个负号
                    e.Handled = false;
                if (e.KeyChar == Convert.ToChar((int)Keys.Back)) // 再加上可以用Back Space返回键
                    e.Handled = false;
            }
      

  7.   

    直接在那个里面的属性设置下不就OK,设置成只能输入数字的~~CONTENTINFO
      

  8.   

    应该是你在输入的时候输入了非float型的数据.所以会报输入了非法数据 只要你输入的是float的数据就不会报错.上面的一代码没用和你的一样
      

  9.   

    这种检测输入最好在页面上用正则表达式完成,结省资源。
    ValidationExpression="\d{1,10}?$" ;其中{1,10}表示位数是1到10位
      

  10.   


    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 

        if(!Char.IsDigit(e.KeyChar))
        {
          e.Handled=true;
        }
        else
        {
          e.handled=false;
        }

    //这是只能输入数字看了3楼的方法我想可以改下,就可以判断小数了.
      

  11.   


    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                int indexnum = 0;
                indexnum = textBox1.Text.Length - textBox1.Text.Replace(".", "").Length;
                if (e.KeyChar <= '9' && e.KeyChar >= '0' || e.KeyChar == '.')
                {
                    if (textBox1.Text == "")
                    {
                        if (e.KeyChar == '.')
                        {
                            e.Handled = true;
                        }                }
                    else if (e.KeyChar=='.'&& indexnum > 0)
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    e.Handled = true;
                }
            }
            
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                { }
             }
    //这是我在textbox1的Keypress中写的一个判断只能输入一个小数点和只能输入数字的办法.
    //希望高手告诉我下 indexof的用法.
    //if (e.KeyChar == '.' && textBox1.Text.IndexOf('.') ==-1) // 9楼这样貌似可以输入多个小数点.
    //              e.Handled = false;
      

  12.   

    if (e.KeyChar == '.' && textBox1.Text.IndexOf('.') ==-1) // 9楼这样貌似可以输入多个小数点
             e.Handled = false;怎么会能输入多个,你试过没有啊当你第一次输入小数点的时候,因为之前没有小数点,所以
    textBox1.Text.IndexOf('.') 等于=-1,所以if 是成立的所以e.handle会=false;也就是不中止这次输入的字符加入text,小数点会加入textBox1.text
    而第2次输入的时候因为text里有小数点了,所以textBox1.Text.IndexOf('.') 会大于-1,所以if不会执行,所以e.handle还是Char.IsDigit(e.KeyChar);的结果,也就是true,所以会中止这次输入的字符加入text
    你新建个项目拷过去试试就知道了
      

  13.   

    用TryParse,转换失败会return一个false值。float a, b, c, d;
                if (float.TryParse(textBox1.Text, out a) && float.TryParse(textBox2.Text, out b) && float.TryParse(textBox3.Text, out c))
                {
                    d = (a + b + c) / 3;
                    textBox4.Text = d.ToString();
                    MessageBox.Show(textBox4.Text, "平均分");
                }
      

  14.   


                float a = 1, b, c, d;
                Regex r = new Regex(@"^(-?\d+)(\.\d+)?$");
                if (r.Match(textBox1.Text).Success && r.Match(textBox2.Text).Success && r.Match(textBox3.Text).Success)
                {
                    a = float.Parse(textBox1.Text);
                    b = float.Parse(textBox1.Text);
                    c = float.Parse(textBox3.Text);
                    d = (a + b + c) / 3;
                    textBox4.Text = d.ToString();
                    MessageBox.Show(textBox4.Text, "平均分");
                }
                else { MessageBox.Show("请输入Float型的数"); }
      

  15.   

    用FilteredTextBoxExtender控件, ValidChars="1234567890."