C# 学习之旅(2)--- 意外的收获
今天在完成老师布置的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);
}
}
}
}
正常测试下, 效果如下:
在学长的试用下,任意输入了几个字母,然后程序崩溃了,~~~~(>_<)~~~~ !
所以上网一阵乱搜后,就有了下面的效果 : )
如下:若输入的字符为非数字( 当然首尾负号是可以检测到的 : ) )则会自动将文本框清空,
同时文本框背景置为红色,再输入正确格式后变回原来的颜色。
该效果核心代码如下:
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巴狼 将对自己的一点努力感到非常高兴;由于作者本人水平有限,如果本文档中包含的错误给您造成了不便,在此提前说声抱歉。
祝身体健康,工作顺利。
C# 学习之旅(2)--- 意外的收获的更多相关文章
- WCF学习之旅—第三个示例之二(二十八)
上接WCF学习之旅—第三个示例之一(二十七) 五.在项目BookMgr.Model创建实体类数据 第一步,安装Entity Framework 1) 使用NuGet下载最新版的Entity Fram ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- Hadoop学习之旅二:HDFS
本文基于Hadoop1.X 概述 分布式文件系统主要用来解决如下几个问题: 读写大文件 加速运算 对于某些体积巨大的文件,比如其大小超过了计算机文件系统所能存放的最大限制或者是其大小甚至超过了计算机整 ...
- WCF学习之旅—第三个示例之三(二十九)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) 在上一篇文章中我们创建了实体对象与接口协定,在这一篇文章中我们来学习如何创建WCF的服务端代码.具体步骤见下面. ...
- WCF学习之旅—WCF服务部署到IIS7.5(九)
上接 WCF学习之旅—WCF寄宿前的准备(八) 四.WCF服务部署到IIS7.5 我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以 ...
- WCF学习之旅—WCF服务部署到应用程序(十)
上接 WCF学习之旅—WCF寄宿前的准备(八) WCF学习之旅—WCF服务部署到IIS7.5(九) 五.控制台应用程序宿主 (1) 在解决方案下新建控制台输出项目 ConsoleHosting.如下 ...
- WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...
- WCF学习之旅—WCF服务的WAS寄宿(十二)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...
- WCF学习之旅—WCF服务的批量寄宿(十三)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) WCF学习之旅—WCF ...
- WCF学习之旅—第三个示例之五(三十一)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) WCF学习 ...
随机推荐
- Android(java)学习笔记95:Android原理揭秘系列之View、ViewGroup
作过Android 应用开发的朋友都知道,Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器, ...
- 安卓Design包之TabLayout控件的使用
转自: 安卓Design包之TabLayout控件的简单使用 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android ...
- asp.net 文件压缩zip下载
今天分享下昨天做的一个东西 asp.net 的文件 zip 批量下载,首先你需要去 到http://dotnetzip.codeplex.com这个站点下载zip 的包,在里面找到 Ionic.Z ...
- MyBatis(3.2.3) - Paginated ResultSets using RowBounds
Sometimes, we may need to work with huge volumes of data, such as with tables with millions of recor ...
- Redis - 排序
SORT 命令格式 SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALP ...
- Visual Studio Gallery
Web Essentials :对CSS.JavaScript和HTML都提供了很多快捷的功能支持.http://vswebessentials.com/features/general Web Co ...
- TSQL基础(四) - 日期处理
日期类型-DateTime DateTime是sql中最常用的日期类型. 存储大小为:8个字节: 日期范围:1753-01-01到9999-12-31: 精确度:3.33毫秒: 常用的日期函数 Get ...
- linux学习书籍推荐linux学习书籍推荐
引用地址:http://www.cnblogs.com/notepi/archive/2013/06/15/3137103.html Linux 学习书目推荐 Linux基础 1.<Linux与 ...
- orcale 循环插入 测试数据
以前开发一直用的是sql server 定义临时变量 循环插入数据到表中已经成为一种固定的模式,本来想orcale应该也一样吧 都是数据库.. 结果被现实无情的打击到了.在网上找办法,求大神 最后 ...
- 使用Emmet(前身Zen Coding)加速Web前端开发
Emmet插件以前被称作为Zen Coding,是一个文本编辑器的插件,它可以帮助您快速编写HTML和CSS代码,从而加速Web前端开发.早在2009年,Sergey Chikuyonok写过一篇文章 ...