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. ...
随机推荐
- 【Tip】如何让引用的dll随附的xml注释文档、pdb调试库等文件不出现在项目输出目录中
项目输出目录(bin/debug|release)中经常是这个样子: main.exemain.pdb a.dll a.xml b.dll b.pdb b.xml ... 其中xml是同名dll的注释 ...
- android Can't bind to local 86XX for debugger
For some reason eclipse DDMS always gives the error 'Can't bind to local 86XX for debugger' every ti ...
- ym—— Android网络框架Volley(体验篇)
VolleyGoogle I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- java.net.SocketException: Software caused connection abort: socket write error
用Java客户端程序访问Java Web服务器时出错: java.net.SocketException: Software caused connection abort: socket write ...
- HttpClient通过Post上传多个文件
public static String sendFilesPost(String url, String fileNames) { HttpClient httpClient = null; Htt ...
- appach2.4 + php7 +mysql5.7.14 配置
步骤1.首先打开Apache2.2\conf里面的httpd.conf文件.在里面找到: ServerRoot ,改成Appache所在目录 步骤二 在LoadModule 后面添加支持php7的扩 ...
- 判别或预测方法汇总(判别分析、神经网络、支持向量机SVM等)
%% [Input]:s_train(输入样本数据,行数为样本数,列为维数):s_group(训练样本类别):s_sample(待判别数据)%% [Output]:Cla(预测类别) function ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- B/S结构的流程简单概述
在介绍appl ication 对象之前,先简单介绍一些Web 服务器的实现原理. 对于大部分浏览器而言,它通常负责完成三件事情: (1)向远程服务器发送请求. (2)读取远程服务器返 ...
- Java基础学习 -- 异常
当异常发生时,原本要接着执行的代码不再执行,转而让其他部分的代码来处理.如果没有代码负责处理,控制台会报告异常. 异常出现时的执行机制: 异常机制最大的好处是:清晰地分开了 正常的业务逻辑 和 遇到情 ...