代码:

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace CheckInput
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12.  
  13. private void Sure_button_Click(object sender, EventArgs e)
  14. {
  15.  
  16. if (CheckIsLegal() && CheckIsNull())
  17. {
  18. //TODO
  19. }
  20.  
  21. //just for test
  22. if (CheckIsNull()&&CheckIsLegal_test())
  23. {
  24. //TODO
  25. }
  26. }
  27.  
  28. /// <summary>
  29. /// 判断输入是否合法
  30. /// </summary>
  31. /// <returns></returns>
  32. private bool CheckIsLegal()
  33. {
  34. string[] SpecialString = new string[] { "/", @"\", ":", "*", "?", "<", ">", "|" };
  35. //注:反斜杠“\”是转义字符
  36. //“\'”单引号;“\"”双引号;“\\”反斜杠;“\0”空;“\a”警告;“\b”退格;“\f”换页;“\n”换行;“\r”换行
  37. //注:用@ 符号加在字符串前面表示其中的转义字符“不”被处理
  38. int tempInt = ;
  39.  
  40. for (int i = ; i < SpecialString.Length; i++)
  41. {
  42. if (this.Name_textBox.Text.Trim().Contains(SpecialString[i]))
  43. {
  44. MessageBox.Show(@"姓名不能包含下列字符:/ \ : * ? < > |");
  45. this.Name_textBox.Select();
  46. return false;
  47. }
  48. if (this.Nickname_textBox.Text.Contains(SpecialString[i]))
  49. {
  50. MessageBox.Show(@"昵称不能包含下列字符:/ \ : * ? < > |");
  51. this.Nickname_textBox.Select();
  52. return false;
  53. }
  54. //TODO
  55.  
  56. //其他的输入框同理
  57.  
  58. //TODO
  59. }
  60.  
  61. //注:string输入变成int型:1.int.TryParse;2.Convert.ToInt32();
  62. //注:int转string:1.Convert.ToString();
  63. if (!int.TryParse(this.Age_textBox.Text, out tempInt) || tempInt < )
  64. {
  65. MessageBox.Show("年龄输入错误!");
  66. this.Age_textBox.Select();
  67. return false;
  68. }
  69. //TODO
  70.  
  71. //其他的输入框同理
  72.  
  73. //TODO
  74. else
  75. {
  76. return true;
  77. }
  78. }
  79.  
  80. /// <summary>
  81. /// 判断输入框是否为空
  82. /// </summary>
  83. /// <returns></returns>
  84. private bool CheckIsNull()
  85. {
  86. //Trim()删除字符串头部及尾部出现的空格=>这里判断是否为空,所以必须加上
  87. //删除的过程为从外到内,直到碰到一个非空格的字符为止,所以不管前后有多少个连续的空格都会被删除掉。
  88. //注:TrimStart()=>只删除字符串的头部的空格
  89. //注:TrimEnd()=>只删除字符串尾部的空格
  90. if (this.Name_textBox.Text.Trim()=="")
  91. {
  92. MessageBox.Show(@"姓名不能为空!");
  93. this.Name_textBox.Select();
  94. return false;
  95. }
  96. if (this.Nickname_textBox.Text.Trim() == "")
  97. {
  98. MessageBox.Show(@"昵称不能为空!");
  99. this.Nickname_textBox.Select();
  100. return false;
  101. }
  102. //TODO
  103.  
  104. //其他的输入框同理
  105.  
  106. //TODO
  107. else
  108. {
  109. return true;
  110. }
  111. }
  112.  
  113. /// <summary>
  114. /// 开始不理解 out tempInt 的作用
  115. /// 顺便复习一下string转化为int的过程
  116. /// </summary>
  117. /// <returns></returns>
  118. private bool CheckIsLegal_test()
  119. {
  120. int tempInt = ;
  121.  
  122. //注:Convert.ToInt32
  123.  
  124. if (!int.TryParse(this.Age_textBox.Text, out tempInt) || CheckIntIsNegative(Convert.ToInt32
  125. (int.TryParse(this.Age_textBox.Text, out tempInt))))
  126. {
  127. MessageBox.Show("年龄输入错误!");
  128. this.Age_textBox.Select();
  129. return false;
  130. }
  131. //TODO
  132.  
  133. //其他的输入框同理
  134.  
  135. //TODO
  136. else
  137. {
  138. return true;
  139. }
  140. }
  141.  
  142. private bool CheckIntIsNegative(int m)
  143. {
  144. if (m < )
  145. {
  146. return false;
  147. }
  148. else
  149. {
  150. return true;
  151. }
  152. }
  153.  
  154. private bool CheckDoubleIsNegative(double m)
  155. {
  156. if (m < )
  157. {
  158. return false;
  159. }
  160. else
  161. {
  162. return true;
  163. }
  164. }
  165.  
  166. private void Cancel_button_Click(object sender, EventArgs e)
  167. {
  168. this.Close();
  169. }
  170. }
  171. }

效果图:

C#学习笔记-输入数据判断(int、double、string)的更多相关文章

  1. Java学习笔记之——Manth类和String类

    (1) Math:常用的数学运算,都是静态方法 方法摘要 static double abs(double a) 返回 double 值的绝对值. static float abs(float a) ...

  2. Java学习笔记(二):String

    String 在Java中String是作为引用对象存在的一种数据类型,用来保存字符串. 实例化和赋值 //直接声明 String s1 = "Hello world!"; //通 ...

  3. int/double/string使用

    在计算机中存储数据和儿童在抽屉中存放物品很类似. 例如: 要在计算机中存一个数字50,需要两句话. int a;  //将要放的物品告诉家长 a=50;  //将物品放到某个抽屉中 计算机存储变量的过 ...

  4. Effective STL 学习笔记 Item 16:vector, string & C API

    有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...

  5. Activiti学习笔记11 — 判断节点的使用

    一. 创建流程 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns=" ...

  6. Python学习笔记—条件判断和循环

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  7. Objective-C学习笔记之for( int )机制

    NSArray *myArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4&q ...

  8. Shell学习笔记 - 条件判断式

    1. 判断格式 1) test 参数 文件 例: test -e /root/install.log 2) [ 参数 文件 ]  -- 推荐使用 例: [ -e /root/install.log ] ...

  9. [Android学习笔记]枚举与int的转换

    package com.example.enumdemo; import android.app.Activity; import android.os.Bundle; import android. ...

随机推荐

  1. 【Tip】如何让引用的dll随附的xml注释文档、pdb调试库等文件不出现在项目输出目录中

    项目输出目录(bin/debug|release)中经常是这个样子: main.exemain.pdb a.dll a.xml b.dll b.pdb b.xml ... 其中xml是同名dll的注释 ...

  2. 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 ...

  3. ym—— Android网络框架Volley(体验篇)

    VolleyGoogle I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  4. java.net.SocketException: Software caused connection abort: socket write error

    用Java客户端程序访问Java Web服务器时出错: java.net.SocketException: Software caused connection abort: socket write ...

  5. HttpClient通过Post上传多个文件

    public static String sendFilesPost(String url, String fileNames) { HttpClient httpClient = null; Htt ...

  6. appach2.4 + php7 +mysql5.7.14 配置

    步骤1.首先打开Apache2.2\conf里面的httpd.conf文件.在里面找到: ServerRoot ,改成Appache所在目录  步骤二 在LoadModule 后面添加支持php7的扩 ...

  7. 判别或预测方法汇总(判别分析、神经网络、支持向量机SVM等)

    %% [Input]:s_train(输入样本数据,行数为样本数,列为维数):s_group(训练样本类别):s_sample(待判别数据)%% [Output]:Cla(预测类别) function ...

  8. Longest Substring Without Repeating Characters(C语言实现)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. B/S结构的流程简单概述

    在介绍appl ication 对象之前,先简单介绍一些Web 服务器的实现原理.         对于大部分浏览器而言,它通常负责完成三件事情: (1)向远程服务器发送请求. (2)读取远程服务器返 ...

  10. Java基础学习 -- 异常

    当异常发生时,原本要接着执行的代码不再执行,转而让其他部分的代码来处理.如果没有代码负责处理,控制台会报告异常. 异常出现时的执行机制: 异常机制最大的好处是:清晰地分开了 正常的业务逻辑 和 遇到情 ...