C # learning notes-input data judgment (int, double, string ),
Code:
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 // determine whether the input is valid 30 /// </summary> 31 // <returns> </returns> 32 private bool CheckIsLegal () 33 {34 string [] SpecialString = new string [] {"/", @ "\", ":", "*", "? "," <","> "," | "}; 35 // Note: The Backslash" \ "is the Escape Character 36 //" \ "single quotes; "\" "Double quotation marks;" \ "backslash;" \ 0 "blank;" \ a "warning;" \ B "return;" \ f "form; "\ n" newline; "\ r" newline 37 // Note: add the @ symbol before the string to indicate that the Escape Character "no" is processed 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 (@ "name cannot contain the following characters :/\:*? <> | "); 45 this. name_textBox.Select (); 46 return false; 47} 48 if (this. nickname_textBox.Text.Contains (SpecialString [I]) 49 {50 MessageBox. show (@ "the nickname cannot contain the following characters :/\:*? <> | "); 51 this. nickname_textBox.Select (); 52 return false; 53} 54 // TODO 55 56 // other input boxes are also 57 58 // TODO 59} 60 61 // note: string input to int type: 1.int. tryParse; 2. convert. toInt32 (); 62 // Note: int to string: 1. convert. toString (); 63 if (! Int. TryParse (this. Age_textBox.Text, out tempInt) | tempInt <0) 64 {65 MessageBox. Show ("Age input error! "); 66 this. age_textBox.Select (); 67 return false; 68} 69 // TODO 70 71 // other input boxes likewise 72 73 // TODO 74 else 75 {76 return true; 77} 78} 79 80 // <summary> 81 // check whether the input box is empty. 82 // </summary> 83 // <returns> </returns> 84 private bool CheckIsNull () 85 {86 // Trim (): Delete the space at the header and tail of the string => check whether it is null. Therefore, you must add 87, until a non-space character is encountered, no matter how many consecutive spaces before and after it is deleted. 88 // Note: TrimStart () => Delete only spaces in the string header 89 // Note: TrimEnd () => Delete only spaces at the end of the string 90 if (this. name_textBox.Text.Trim () = "") 91 {92 MessageBox. show (@ "name cannot be blank! "); 93 this. name_textBox.Select (); 94 return false; 95} 96 if (this. nickname_textBox.Text.Trim () = "") 97 {98 MessageBox. show (@ "nickname cannot be blank! "); 99 this. nickname_textBox.Select (); 100 return false; 101} 102 // TODO103 104 // other input boxes are also 105 106 // TODO107 else108 {109 return true; 110} 111} 112 113 114 // <summary> 115 // do not understand the function of out tempInt at the beginning 116 // By the way, review the process of converting string to int 117 // /</summary> 118 // <returns> </returns> 119 private bool CheckIsLegal_test () 120 {121 int tempInt = 0; 122 123 // Note: Convert. toin32124 125 if (! Int. tryParse (this. age_textBox.Text, out tempInt) | CheckIntIsNegative (Convert. toInt32126 (int. tryParse (this. age_textBox.Text, out tempInt) 127 {128 MessageBox. show ("Age input error! "); 129 this. age_textBox.Select (); 130 return false; 131} 132 // todow.134 // other input boxes are also 135 136 // todow.else138 {139 return true; 140} 141} 142 143 private bool CheckIntIsNegative (int m) 144 {145 if (m <0) 146 {147 return false; 148} 149 else150 {151 return true; 152} 153} 154 155 private bool CheckDoubleIsNegative (double m) 156 {157 if (m <0) 158 {159 return false; 160} 161 else162 {163 return true; 164} 165} 166 167 private void Cancel_button_Click (object sender, EventArgs e) 168 {169 this. close (); 170} 171} 172}
: