C#学习笔记-输入数据判断(int、double、string)
代码:
using System;
using System.Windows.Forms; namespace CheckInput
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Sure_button_Click(object sender, EventArgs e)
{ if (CheckIsLegal() && CheckIsNull())
{
//TODO
} //just for test
if (CheckIsNull()&&CheckIsLegal_test())
{
//TODO
}
} /// <summary>
/// 判断输入是否合法
/// </summary>
/// <returns></returns>
private bool CheckIsLegal()
{
string[] SpecialString = new string[] { "/", @"\", ":", "*", "?", "<", ">", "|" };
//注:反斜杠“\”是转义字符
//“\'”单引号;“\"”双引号;“\\”反斜杠;“\0”空;“\a”警告;“\b”退格;“\f”换页;“\n”换行;“\r”换行
//注:用@ 符号加在字符串前面表示其中的转义字符“不”被处理
int tempInt = ; for (int i = ; i < SpecialString.Length; i++)
{
if (this.Name_textBox.Text.Trim().Contains(SpecialString[i]))
{
MessageBox.Show(@"姓名不能包含下列字符:/ \ : * ? < > |");
this.Name_textBox.Select();
return false;
}
if (this.Nickname_textBox.Text.Contains(SpecialString[i]))
{
MessageBox.Show(@"昵称不能包含下列字符:/ \ : * ? < > |");
this.Nickname_textBox.Select();
return false;
}
//TODO //其他的输入框同理 //TODO
} //注:string输入变成int型:1.int.TryParse;2.Convert.ToInt32();
//注:int转string:1.Convert.ToString();
if (!int.TryParse(this.Age_textBox.Text, out tempInt) || tempInt < )
{
MessageBox.Show("年龄输入错误!");
this.Age_textBox.Select();
return false;
}
//TODO //其他的输入框同理 //TODO
else
{
return true;
}
} /// <summary>
/// 判断输入框是否为空
/// </summary>
/// <returns></returns>
private bool CheckIsNull()
{
//Trim()删除字符串头部及尾部出现的空格=>这里判断是否为空,所以必须加上
//删除的过程为从外到内,直到碰到一个非空格的字符为止,所以不管前后有多少个连续的空格都会被删除掉。
//注:TrimStart()=>只删除字符串的头部的空格
//注:TrimEnd()=>只删除字符串尾部的空格
if (this.Name_textBox.Text.Trim()=="")
{
MessageBox.Show(@"姓名不能为空!");
this.Name_textBox.Select();
return false;
}
if (this.Nickname_textBox.Text.Trim() == "")
{
MessageBox.Show(@"昵称不能为空!");
this.Nickname_textBox.Select();
return false;
}
//TODO //其他的输入框同理 //TODO
else
{
return true;
}
} /// <summary>
/// 开始不理解 out tempInt 的作用
/// 顺便复习一下string转化为int的过程
/// </summary>
/// <returns></returns>
private bool CheckIsLegal_test()
{
int tempInt = ; //注:Convert.ToInt32 if (!int.TryParse(this.Age_textBox.Text, out tempInt) || CheckIntIsNegative(Convert.ToInt32
(int.TryParse(this.Age_textBox.Text, out tempInt))))
{
MessageBox.Show("年龄输入错误!");
this.Age_textBox.Select();
return false;
}
//TODO //其他的输入框同理 //TODO
else
{
return true;
}
} private bool CheckIntIsNegative(int m)
{
if (m < )
{
return false;
}
else
{
return true;
}
} private bool CheckDoubleIsNegative(double m)
{
if (m < )
{
return false;
}
else
{
return true;
}
} private void Cancel_button_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
效果图:


C#学习笔记-输入数据判断(int、double、string)的更多相关文章
- Java学习笔记之——Manth类和String类
(1) Math:常用的数学运算,都是静态方法 方法摘要 static double abs(double a) 返回 double 值的绝对值. static float abs(float a) ...
- Java学习笔记(二):String
String 在Java中String是作为引用对象存在的一种数据类型,用来保存字符串. 实例化和赋值 //直接声明 String s1 = "Hello world!"; //通 ...
- int/double/string使用
在计算机中存储数据和儿童在抽屉中存放物品很类似. 例如: 要在计算机中存一个数字50,需要两句话. int a; //将要放的物品告诉家长 a=50; //将物品放到某个抽屉中 计算机存储变量的过 ...
- Effective STL 学习笔记 Item 16:vector, string & C API
有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...
- Activiti学习笔记11 — 判断节点的使用
一. 创建流程 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns=" ...
- Python学习笔记—条件判断和循环
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...
- Objective-C学习笔记之for( int )机制
NSArray *myArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4&q ...
- Shell学习笔记 - 条件判断式
1. 判断格式 1) test 参数 文件 例: test -e /root/install.log 2) [ 参数 文件 ] -- 推荐使用 例: [ -e /root/install.log ] ...
- [Android学习笔记]枚举与int的转换
package com.example.enumdemo; import android.app.Activity; import android.os.Bundle; import android. ...
随机推荐
- spring aop配置出错
Multiple annotations found at this line: - schema_reference.4: Failed to read schema document 'http: ...
- arcgis for flex全国地图天气预报的具体实现过程解析
系统架构是B/S,开发语言是flex,开发工具是myeclise或者flashbuild,通过调用百度提供的在线天气预报web api接口的方式来实现. 采用地图是ArcGIS全国地图,开发接口为ar ...
- 心无旁骛,向死而生:WGDC2016给创企上的一堂课
"这是最好的时代,也是最坏的时代:这是希望的春天,也是失望的冬天." ------狄更斯 WGDC2016落幕已经一月有余,我仍然记得会议结束后,穿过高大宽敞的国家会议中心大厅,走 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q128-Q130)
Question 128 You are designing a SharePoint 2010 solution that includes a custom site definition an ...
- android gradle NDK简介
本章介绍在Android开发中,关于NDK,gradle相关的知识点. 1.NDK简介 (1)NDK是一系列工具的集合 NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将s ...
- Play Framework 完整实现一个APP(六)
需要为Blog添加 查看和发表评论的功能 1.创建查看功能 Application.java中添加 show() 方法 public static void show(Long id) { Post ...
- MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子
什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...
- ORA-06502:PL/SQL :numberic or value error: character string buffer too small
今天遇到一个错误提示:ORA-06502:PL/SQL :numberic or value error: character string buffer too small,一般对应的中文信息为:O ...
- SSRS Reports 2008性能优化案例二
前几天一同事反映海外工厂A的SSRS报表比较慢,让我检查优化一下.于是我检查了下2015-07-13到2015-07-15 12:00这段时间报表的耗时记录 USE [ReportServer]; ...
- .NET应用架构设计—表模块模式与事务脚本模式的代码编写
阅读目录: 1.背景介绍 2.简单介绍表模块模式.事务脚本模式 3.正确的编写表模块模式.事务脚本模式的代码 4.总结 1.背景介绍 要想正确的设计系统架构就必须能正确的搞懂每个架构模式的用意,而不是 ...