今天在完成老师布置的C#作业(计算一元二次方程的根)的时候,收获到意外的知识,所以写此博文予以记录。

意外收获为: 对文本框的输入值进行检测,使之按照要求格式输入。

下面是整个的源代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void textBox1_TextChanged(object sender, EventArgs e)
{
string str1 = textBox1.Text.Trim(); for (int i = ; i < str1.Length; i++)
{
if (i == && str1[] == '-')
{
continue;
}
if (!Char.IsNumber(str1[i]) || (i == && str1[] == ''))
{
textBox1.Text = string.Empty;
textBox1.BackColor = Color.Red;
}
else
{
textBox1.BackColor = Color.Empty;
}
}
// if (textBox1.Text == "")
// textBox1.ForeColor = Color.Red;
} private void textBox2_TextChanged(object sender, EventArgs e)
{
string str2 = textBox2.Text.Trim(); for (int i = ; i < str2.Length; i++)
{
if (i == && str2[] == '-')
{
continue;
}
if (!Char.IsNumber(str2[i]))
{
textBox2.Text = string.Empty;
textBox2.BackColor = Color.Red;
}
else
{
textBox2.BackColor = Color.Empty;
}
}
} private void textBox3_TextChanged(object sender, EventArgs e)
{
string str3 = textBox3.Text.Trim(); for (int i = ; i < str3.Length; i++)
{
if (i == && str3[] == '-')
{
continue;
}
if (!Char.IsNumber(str3[i]))
{
textBox3.Text = string.Empty;
textBox3.BackColor = Color.Red;
}
else
{
textBox3.BackColor = Color.Empty;
}
}
} private void Confirm_Click(object sender, EventArgs e)
{ // float a = float.Parse(textBox1.Text);
// float b = float.Parse(textBox2.Text);
// float c = float.Parse(textBox3.Text); object box1 = textBox1;
object box2 = textBox2;
object box3 = textBox3;
object box4 = textBox4; Demo ch = new Demo(); ch.set(box1,box2,box3);
ch.jiSuan();
ch.show(box4);
}
} public class Demo
{
private float a = ;
private float b = ;
private float c = ;
public double x = ;
public double y = ;
public double r = ;
public double i = ;
public int flag; public void set(object box1,object box2,object box3)
{
TextBox tempa = (TextBox)box1;
TextBox tempb = (TextBox)box2;
TextBox tempc = (TextBox)box3; if (tempa.Text == string.Empty )
tempa.Text = string.Format("请输入数字");
else
{
this.a = float.Parse(tempa.Text);
} if (tempb.Text == string.Empty)
tempb.Text = string.Format("");
else
{
this.b = float.Parse(tempb.Text);
} if (tempc.Text == string.Empty)
tempc.Text = string.Format("");
else
{
this.c = float.Parse(tempc.Text);
} } public void jiSuan()
{
double demo = ;
demo = Math.Pow(b, ) - * a * c;
if (demo == )
{
flag = ;
x = y = -b / ( * a);
}
else if (demo > )
{
flag = ;
x = (-b + Math.Sqrt(demo)) / ( * a);
y = (-b - Math.Sqrt(demo)) / ( * a);
}
else if (demo < )
{
flag = ;
r = -b / ( * a);
i = Math.Sqrt( * a * c - b * b) / ( * a);
}
} public void show(object box4)
{
TextBox temp = (TextBox)box4; if (this.a == )
{
temp.Text = string.Format("参数输入有误");
}
else
{
if (flag == )
temp.Text = String.Format("x = y = {0:N2}", x);
else if (flag == )
temp.Text = String.Format("x = {0:N2}, y = {1:N2}", x, y);
else if (flag == )
temp.Text = String.Format("x = {0:N2}i + {1:N2}, y = -{2:N2}i + {3:N2}", i, r, i, r);
}
}
}
}

正常测试下, 效果如下:

C# 学习之旅(2)---  意外的收获-LMLPHP

在学长的试用下,任意输入了几个字母,然后程序崩溃了,~~~~(>_<)~~~~ !

所以上网一阵乱搜后,就有了下面的效果 : )

如下:若输入的字符为非数字( 当然首尾负号是可以检测到的 : ) )则会自动将文本框清空,

同时文本框背景置为红色,再输入正确格式后变回原来的颜色。

C# 学习之旅(2)---  意外的收获-LMLPHP

该效果核心代码如下:

 private void textBox1_TextChanged(object sender, EventArgs e)
{
string str1 = textBox1.Text.Trim(); for (int i = ; i < str1.Length; i++)
{
if (i == && str1[] == '-')
continue;
if (!Char.IsNumber(str1[i]))
{
textBox1.Text = string.Empty;
textBox1.BackColor = Color.Red;
}
else
{
textBox1.BackColor = Color.Empty;
}
}
}

读到这里,您辛苦了!  ^_^

————————————————————————————————————————————————————————————————————————————

声明:

  本文为 大Yi巴狼 对自己所学的知识整理和实现。

  本文档欢迎自由转载,但请务必保持本文档完整或注明来之本文档。本文档未经 大Yi巴狼 同意,不得用于商业用途。最后,如果您能从这个简单文档里获得些许帮助,大Yi巴狼 将对自己的一点努力感到非常高兴;由于作者本人水平有限,如果本文档中包含的错误给您造成了不便,在此提前说声抱歉。

  祝身体健康,工作顺利。

05-11 09:38