C# 学习之旅(2)--- 意外的收获
时间:2014-03-05 17:16:59
收藏:0
阅读:302
今天在完成老师布置的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 continue; 27 if (!Char.IsNumber(str1[i])) 28 { 29 textBox1.Text = string.Empty; 30 textBox1.BackColor = Color.Red; 31 } 32 else 33 { 34 textBox1.BackColor = Color.Empty; 35 } 36 } 37 // if (textBox1.Text == "") 38 // textBox1.ForeColor = Color.Red; 39 } 40 41 private void textBox2_TextChanged(object sender, EventArgs e) 42 { 43 string str2 = textBox2.Text.Trim(); 44 45 for (int i = 0; i < str2.Length; i++) 46 { 47 if (i == 0 && str2[0] == ‘-‘) 48 continue; 49 if (!Char.IsNumber(str2[i])) 50 { 51 textBox2.Text = string.Empty; 52 textBox2.BackColor = Color.Red; 53 } 54 else 55 { 56 textBox2.BackColor = Color.Empty; 57 } 58 } 59 } 60 61 private void textBox3_TextChanged(object sender, EventArgs e) 62 { 63 string str3 = textBox3.Text.Trim(); 64 65 for (int i = 0; i < str3.Length; i++) 66 { 67 if (i == 0 && str3[0] == ‘-‘) 68 continue; 69 if (!Char.IsNumber(str3[i])) 70 { 71 textBox3.Text = string.Empty; 72 textBox3.BackColor = Color.Red; 73 } 74 else 75 { 76 textBox3.BackColor = Color.Empty; 77 } 78 } 79 } 80 81 private void Confirm_Click(object sender, EventArgs e) 82 { 83 float a = float.Parse(textBox1.Text); 84 float b = float.Parse(textBox2.Text); 85 float c = float.Parse(textBox3.Text); 86 87 Demo ch = new Demo(a, b, c); 88 ch.jiSuan(); 89 if (ch.flag == 0) 90 textBox4.Text = String.Format("x = y = {0}", ch.x); 91 else if (ch.flag == 1) 92 textBox4.Text = String.Format("x = {0:N2}, y = {1:N2}", ch.x, ch.y); 93 else if (ch.flag == 2) 94 textBox4.Text = String.Format("x = {0:N2}i + {1}, y = -{2:N2}i + {3}", ch.i, ch.r, ch.i, ch.r); 95 } 96 97 public class Demo 98 { 99 private float a = 0; 100 private float b = 0; 101 private float c = 0; 102 public double x = 0; 103 public double y = 0; 104 public double r = 0; 105 public double i = 0; 106 public int flag; 107 108 public Demo(float a, float b, float c) 109 { 110 this.a = a; 111 this.b = b; 112 this.c = c; 113 } 114 115 public void jiSuan() 116 { 117 double demo = 0; 118 demo = Math.Pow(b, 2) - 4 * a * c; 119 if (demo == 0) 120 { 121 flag = 0; 122 x = y = -b / (2 * a); 123 } 124 else if (demo > 0) 125 { 126 flag = 1; 127 x = (-b + Math.Sqrt(demo)) / (2 * a); 128 y = (-b - Math.Sqrt(demo)) / (2 * a); 129 } 130 else if (demo < 0) 131 { 132 flag = 2; 133 r = -b / (2 * a); 134 i = Math.Sqrt(4 * a * c - b * b) / (2 * a); 135 } 136 } 137 138 /* public void show() 139 { 140 if (flag == 0) 141 System.Console.WriteLine(String.Format("x = y ={0}", x)); 142 else if (flag == 1) 143 System.Console.WriteLine(String.Format("x = {0:N3}, y = {1:N3}", x, y)); 144 else if (flag == 2) 145 System.Console.WriteLine(String.Format("x = {1}" + "{0:N3}i", "y = {3}"+"{2:N3}i", r, i, r, i)); 146 }*/ 147 } 148 } 149 }
正常测试下, 效果如下:
在学长的试用下,任意输入了几个字母,然后程序崩溃了,~~~~(>_<)~~~~ !
所以上网一阵乱搜后,就有了下面的效果 : )
如下:若输入的字符为非数字( 当然首尾负号是可以检测到的 : ) )则会自动将文本框清空,
同时文本框背景置为红色,再输入正确格式后变回原来的颜色。
该效果核心代码如下:
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巴狼 将对自己的一点努力感到非常高兴;由于作者本人水平有限,如果本文档中包含的错误给您造成了不便,在此提前说声抱歉。
祝身体健康,工作顺利。
C# 学习之旅(2)--- 意外的收获,布布扣,bubuko.com
原文:http://www.cnblogs.com/kba977/p/3581173.html
评论(0)