今天在完成老师布置的C#作业(计算一元二次方程的根)的时候,收获到意外的知识,所以写此博文予以记录。
意外收获为: 对文本框的输入值进行检测,使之按照要求格式输入。
下面是整个的源代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Demo 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void textBox1_TextChanged(object sender, EventArgs e) 20 { 21 string str1 = textBox1.Text.Trim(); 22 23 for (int i = 0; i < str1.Length; i++) 24 { 25 if (i == 0 && str1[0] == '-') 26 { 27 continue; 28 } 29 if (!Char.IsNumber(str1[i]) || (i == 0 && str1[0] == '0')) 30 { 31 textBox1.Text = string.Empty; 32 textBox1.BackColor = Color.Red; 33 } 34 else 35 { 36 textBox1.BackColor = Color.Empty; 37 } 38 } 39 // if (textBox1.Text == "") 40 // textBox1.ForeColor = Color.Red; 41 } 42 43 private void textBox2_TextChanged(object sender, EventArgs e) 44 { 45 string str2 = textBox2.Text.Trim(); 46 47 for (int i = 0; i < str2.Length; i++) 48 { 49 if (i == 0 && str2[0] == '-') 50 { 51 continue; 52 } 53 if (!Char.IsNumber(str2[i])) 54 { 55 textBox2.Text = string.Empty; 56 textBox2.BackColor = Color.Red; 57 } 58 else 59 { 60 textBox2.BackColor = Color.Empty; 61 } 62 } 63 } 64 65 private void textBox3_TextChanged(object sender, EventArgs e) 66 { 67 string str3 = textBox3.Text.Trim(); 68 69 for (int i = 0; i < str3.Length; i++) 70 { 71 if (i == 0 && str3[0] == '-') 72 { 73 continue; 74 } 75 if (!Char.IsNumber(str3[i])) 76 { 77 textBox3.Text = string.Empty; 78 textBox3.BackColor = Color.Red; 79 } 80 else 81 { 82 textBox3.BackColor = Color.Empty; 83 } 84 } 85 } 86 87 private void Confirm_Click(object sender, EventArgs e) 88 { 89 90 // float a = float.Parse(textBox1.Text); 91 // float b = float.Parse(textBox2.Text); 92 // float c = float.Parse(textBox3.Text); 93 94 object box1 = textBox1; 95 object box2 = textBox2; 96 object box3 = textBox3; 97 object box4 = textBox4; 98 99 Demo ch = new Demo(); 100 101 ch.set(box1,box2,box3); 102 ch.jiSuan(); 103 ch.show(box4); 104 } 105 } 106 107 public class Demo 108 { 109 private float a = 0; 110 private float b = 0; 111 private float c = 0; 112 public double x = 0; 113 public double y = 0; 114 public double r = 0; 115 public double i = 0; 116 public int flag; 117 118 public void set(object box1,object box2,object box3) 119 { 120 TextBox tempa = (TextBox)box1; 121 TextBox tempb = (TextBox)box2; 122 TextBox tempc = (TextBox)box3; 123 124 if (tempa.Text == string.Empty ) 125 tempa.Text = string.Format("请输入数字"); 126 else 127 { 128 this.a = float.Parse(tempa.Text); 129 } 130 131 if (tempb.Text == string.Empty) 132 tempb.Text = string.Format("0"); 133 else 134 { 135 this.b = float.Parse(tempb.Text); 136 } 137 138 if (tempc.Text == string.Empty) 139 tempc.Text = string.Format("0"); 140 else 141 { 142 this.c = float.Parse(tempc.Text); 143 } 144 145 } 146 147 148 public void jiSuan() 149 { 150 double demo = 0; 151 demo = Math.Pow(b, 2) - 4 * a * c; 152 if (demo == 0) 153 { 154 flag = 0; 155 x = y = -b / (2 * a); 156 } 157 else if (demo > 0) 158 { 159 flag = 1; 160 x = (-b + Math.Sqrt(demo)) / (2 * a); 161 y = (-b - Math.Sqrt(demo)) / (2 * a); 162 } 163 else if (demo < 0) 164 { 165 flag = 2; 166 r = -b / (2 * a); 167 i = Math.Sqrt(4 * a * c - b * b) / (2 * a); 168 } 169 } 170 171 public void show(object box4) 172 { 173 TextBox temp = (TextBox)box4; 174 175 if (this.a == 0) 176 { 177 temp.Text = string.Format("参数输入有误"); 178 } 179 else 180 { 181 if (flag == 0) 182 temp.Text = String.Format("x = y = {0:N2}", x); 183 else if (flag == 1) 184 temp.Text = String.Format("x = {0:N2}, y = {1:N2}", x, y); 185 else if (flag == 2) 186 temp.Text = String.Format("x = {0:N2}i + {1:N2}, y = -{2:N2}i + {3:N2}", i, r, i, r); 187 } 188 } 189 } 190 }
正常测试下, 效果如下:
在学长的试用下,任意输入了几个字母,然后程序崩溃了,~~~~(>_<)~~~~ !
所以上网一阵乱搜后,就有了下面的效果 : )
如下:若输入的字符为非数字( 当然首尾负号是可以检测到的 : ) )则会自动将文本框清空,
同时文本框背景置为红色,再输入正确格式后变回原来的颜色。
该效果核心代码如下:
1 private void textBox1_TextChanged(object sender, EventArgs e) 2 { 3 string str1 = textBox1.Text.Trim(); 4 5 for (int i = 0; i < str1.Length; i++) 6 { 7 if (i == 0 && str1[0] == '-') 8 continue; 9 if (!Char.IsNumber(str1[i])) 10 { 11 textBox1.Text = string.Empty; 12 textBox1.BackColor = Color.Red; 13 } 14 else 15 { 16 textBox1.BackColor = Color.Empty; 17 } 18 } 19 }
读到这里,您辛苦了! ^_^
————————————————————————————————————————————————————————————————————————————
声明:
本文为 大Yi巴狼 对自己所学的知识整理和实现。
本文档欢迎自由转载,但请务必保持本文档完整或注明来之本文档。本文档未经 大Yi巴狼 同意,不得用于商业用途。最后,如果您能从这个简单文档里获得些许帮助,大Yi巴狼 将对自己的一点努力感到非常高兴;由于作者本人水平有限,如果本文档中包含的错误给您造成了不便,在此提前说声抱歉。
祝身体健康,工作顺利。