写了个求平均分的简单代码 在文本框里输入空值或则字母 会弹错误 代码里很简单的申明了一下 怎么样才能输入不符合值的时候不报错和不进行运算呢 我的代码是
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,"平均分");
有人能帮我解说下吗 举下例子更好 谢谢!
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,"平均分");
有人能帮我解说下吗 举下例子更好 谢谢!
解决方案 »
- winform 加载网页flash
- 求助winform
- FORM Timer的问题???判断文本输入内容
- 帮我改改这段代码,js找不到变量,
- 如何在winform中把datagrid变成模板列?
- C# Dictionar根据键值查找值得函数是什么?怎么用?
- 有人用过VisualStudio2005 的office 项目模版开发过word模版吗?
- 如何遍历web页面中的所有控件
- ###############请问那位高人有PockPC上录音的源代码(C#),小弟不胜感激~!####送分了############
- 怎样获得我在dataGrid选中的所有行的行号?
- 发布程序的问题
- C#如何判断某个数据库xdb是否存在
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,"平均分");
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,"平均分");
#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表示只能输入整数
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !Char.IsDigit(e.KeyChar); // 只能输入数字
}
这样的小数点就不能输入了。这样不太好。
楼主定义float a, b, c, d; 肯定要输入小数的。
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;
}
ValidationExpression="\d{1,10}?$" ;其中{1,10}表示位数是1到10位
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsDigit(e.KeyChar))
{
e.Handled=true;
}
else
{
e.handled=false;
}
}
//这是只能输入数字看了3楼的方法我想可以改下,就可以判断小数了.
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;
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
你新建个项目拷过去试试就知道了
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, "平均分");
}
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型的数"); }