C#學習筆記-輸入數據判斷(int、double、string)


代碼:

  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 = 0;
 39 
 40             for (int i = 0; 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 < 0) 
 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 
114         /// <summary>
115         /// 開始不理解 out tempInt 的作用
116         /// 順便復習一下string轉化為int的過程
117         /// </summary>
118         /// <returns></returns>
119         private bool CheckIsLegal_test()
120         {
121             int tempInt = 0;
122 
123             //注:Convert.ToInt32
124 
125             if (!int.TryParse(this.Age_textBox.Text, out tempInt) || CheckIntIsNegative(Convert.ToInt32
126                 (int.TryParse(this.Age_textBox.Text, out tempInt))))
127             {
128                 MessageBox.Show("年齡輸入錯誤!");
129                 this.Age_textBox.Select();
130                 return false;
131             }
132             //TODO
133 
134             //其他的輸入框同理
135 
136             //TODO
137             else
138             {
139                 return true;
140             }
141         }
142 
143         private bool CheckIntIsNegative(int m)
144         {
145             if (m < 0)
146             {
147                 return false;
148             }
149             else
150             {
151                 return true;
152             }
153         }
154 
155         private bool CheckDoubleIsNegative(double m)
156         {
157             if (m < 0)
158             {
159                 return false;
160             }
161             else
162             {
163                 return true;
164             }
165         }
166 
167         private void Cancel_button_Click(object sender, EventArgs e)
168         {
169             this.Close();
170         }
171     }
172 }

 


效果圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM