IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

September Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#   Print This     
 
Useful C# Windows Forms Input Masks
 
Posted 10 months ago on 11/13/2020
Take Away:

This article discusses three C# input masks I have used in my program design.

KB104918

I always have a need to create input masks for my C# data entry screens. They are used to format entered data for text boxes that hold quantities, telephone numbers, currencies, dates, etc.

First, here is an example of an input mask for a date in mm/dd/yy format. The fire_DateEntered function is triggered whenever the user types something into the TransactionDate.Text text box.

  1. // input mask formatting for the new transaction date. this will trigger  
  2. // with each successive keystroke as the user enters the date characters. 
  3. private void fire_DateEntered(object sender, KeyEventArgs e)  
  4. {  
  5. int formaterrorflag;  
  6. string dateinputstring;  
  7. char[] datecharacterarray;  
  8. // get the data from the “TransactionDate.Text” text  
  9. // box and convert to character array for further processing.        
  10. dateinputstring = TransactionDate.Text;  
  11. datecharacterarray = dateinputstring.ToCharArray(0, dateinputstring.Length);  
  12. formaterrorflag = 0;  
  13. // next, compare each of the 8 chars to make sure they
  14. // conform to the “mm/dd/yy” date input mask.    
  15. if(e.KeyValue != 8 && e.KeyValue != 46)  
  16. {  
  17. if(dateinputstring.Length >= 1)  
  18. {  
  19. if((datecharacterarray[0] < (char)48 || datecharacterarray[0] > (char)49) && datecharacterarray[0] > (char)32) formaterrorflag = 1;  
  20. }  
  21. if(dateinputstring.Length >= 2)  
  22. {  
  23. if((datecharacterarray[1] < (char)48 || datecharacterarray[1] > (char)57) && datecharacterarray[1] > (char)32) formaterrorflag = 2;  
  24. }  
  25. if(dateinputstring.Length >= 3)  
  26. {  
  27. if(datecharacterarray[2] != (char)47) formaterrorflag = 22;  
  28. }  
  29. if(dateinputstring.Length >= 4)  
  30. {  
  31. if((datecharacterarray[3] < (char)48 || datecharacterarray[3] > (char)51) && datecharacterarray[3] > (char)32) formaterrorflag = 3;  
  32. }  
  33. if(dateinputstring.Length >= 5)  
  34. {  
  35. if((datecharacterarray[4] < (char)48 || datecharacterarray[4] > (char)57) && datecharacterarray[4] > (char)32) formaterrorflag = 4;  
  36. }  
  37. if(dateinputstring.Length >= 6)  
  38. {  
  39. if(datecharacterarray[5] != (char)47) formaterrorflag = 55;  
  40. }  
  41. if(dateinputstring.Length >= 7)  
  42. {   
  43. if((datecharacterarray[6] < (char)48 || datecharacterarray[6] > (char)57) && datecharacterarray[6] > (char)32) formaterrorflag = 5;  
  44. }   
  45. if(dateinputstring.Length == 8)  
  46. {  
  47. if((datecharacterarray[7] < (char)48 || datecharacterarray[7] > (char)57) && datecharacterarray[7] > (char)32) formaterrorflag = 6;  
  48. }  
  49. }  
  50. // if the user is at the third or the sixth character slot in
  51. // the data entry process, then auto-insert the forward slash
  52. // character so the user doesn’t have to do it.          
  53. if(formaterrorflag == 0)  
  54. {  
  55. if(dateinputstring.Length == 2)  
  56. {  
  57. dateinputstring = dateinputstring + "/";  
  58. this.TransactionDate.Text = dateinputstring;  
  59. this.TransactionDate.SelectionStart = dateinputstring.Length;  
  60. }  
  61. if(dateinputstring.Length == 5)  
  62. {  
  63. dateinputstring = dateinputstring + "/";  
  64. this.TransactionDate.Text = dateinputstring;  
  65. this.TransactionDate.SelectionStart = dateinputstring.Length;  
  66. }  
  67. }  
  68. // if we are at the end of the data entry, then jump to the next field.
  69. if(formaterrorflag == 0 && dateinputstring.Length == 7)  
  70. {  
  71. this.nexttextBox.Focus();  
  72. }  
  73. // if the user entered a period character, then blank
  74. // what has been typed and make the user start over with
  75. // something that will hopefully conform to the input mask we are using.
  76. if(formaterrorflag > 0 || e.KeyValue == 46)  
  77. {  
  78. this.TransactionDate.Text.Remove(0, dateinputstring.Length);  
  79. this.TransactionDate.Text = "";  
  80. }  
  81. }  

Moving on, here is an input mask for a telephone number. The fire_phone_number function is fired whenever the user types something into the PhoneNumber.Text text box.

  1. // input mask formatting for the phone number. this will trigger with each
  2. // successive keystroke as the user enters the phone number digits.     
  3. privatevoid fire_phone_number(object sender, System.Windows.Forms.KeyEventArgs e)  
  4. {  
  5. int formaterrorflag;  
  6. string phoneinputstring;  
  7. char[] phonecharacterarray;  
  8. // get the data from the “PhoneNumber.Text” text
  9. // box and convert to character array for further processing.       
  10. phoneinputstring = PhoneNumber.Text;  
  11. phonecharacterarray = phoneinputstring.ToCharArray(0, phoneinputstring.Length);  
  12. formaterrorflag = 0;  
  13. // next, compare each of the 12 chars to make sure they
  14. // conform to the “999-999-9999” phone number input mask.   
  15. if(e.KeyValue != 8 && e.KeyValue != 46)  
  16. {  
  17. if(phoneinputstring.Length >= 1)  
  18. {  
  19. if((phonecharacterarray[0] < (char)48 || phonecharacterarray[0] > (char)57) && phonecharacterarray[0] > (char)32) formaterrorflag = 1;  
  20. }  
  21. if(phoneinputstring.Length >= 2)  
  22. {  
  23. if((phonecharacterarray[1] < (char)48 || phonecharacterarray[1] > (char)57) && phonecharacterarray[1] > (char)32) formaterrorflag = 2;  
  24. }  
  25. if(phoneinputstring.Length >= 3)  
  26. {  
  27. if((phonecharacterarray[2] < (char)48 || phonecharacterarray[2] > (char)57) && phonecharacterarray[2] > (char)32) formaterrorflag = 3;  
  28. }  
  29. if(phoneinputstring.Length >= 4)  
  30. {  
  31. if(phonecharacterarray[3] != (char)45) formaterrorflag = 33;  
  32. }  
  33. if(phoneinputstring.Length >= 5)  
  34. {  
  35. if((phonecharacterarray[4] < (char)48 || phonecharacterarray[4] > (char)57) && phonecharacterarray[4] > (char)32) formaterrorflag = 4;  
  36. }  
  37. if(phoneinputstring.Length >= 6)  
  38. {  
  39. if((phonecharacterarray[5] < (char)48 || phonecharacterarray[5] > (char)57) && phonecharacterarray[5] > (char)32) formaterrorflag = 5;  
  40. }  
  41. if(phoneinputstring.Length >= 7)  
  42. {  
  43. if((phonecharacterarray[6] < (char)48 || phonecharacterarray[6] > (char)57) && phonecharacterarray[6] > (char)32) formaterrorflag = 6;  
  44. }  
  45. if(phoneinputstring.Length >= 8)  
  46. {  
  47. if(phonecharacterarray[7] != (char)45) formaterrorflag = 66;  
  48. }  
  49. if(phoneinputstring.Length >= 9)  
  50. {  
  51. if((phonecharacterarray[8] < (char)48 || phonecharacterarray[8] > (char)57) && phonecharacterarray[8] > (char)32) formaterrorflag = 7;  
  52. }  
  53. if(phoneinputstring.Length == 10)  
  54. {  
  55. if((phonecharacterarray[9] < (char)48 || phonecharacterarray[9] > (char)57) && phonecharacterarray[9] > (char)32) formaterrorflag = 8;  
  56. }  
  57. if(phoneinputstring.Length == 11)  
  58. {  
  59. if((phonecharacterarray[10] < (char)48 || phonecharacterarray[10] > (char)57) && phonecharacterarray[10] > (char)32) formaterrorflag = 9;  
  60. }  
  61. if(phoneinputstring.Length == 12)  
  62. {  
  63. if((phonecharacterarray[11] < (char)48 || phonecharacterarray[11] > (char)57) && phonecharacterarray[11] > (char)32) formaterrorflag = 10;  
  64. }  
  65. }  
  66. // if the user is at the fourth or the eigth character slot in
  67. // the data entry process, then auto-insert the hyphen
  68. // character so the user doesn’t have to do it.         
  69. if(formaterrorflag == 0)  
  70. {  
  71. if(phoneinputstring.Length == 3)  
  72. {  
  73. phoneinputstring = phoneinputstring + "-";  
  74. this.PhoneNumber.Text = phoneinputstring;  
  75. this.PhoneNumber.SelectionStart = phoneinputstring.Length;  
  76. }  
  77. if(phoneinputstring.Length == 7)  
  78. {  
  79. phoneinputstring = phoneinputstring + "-";  
  80. this.PhoneNumber.Text = phoneinputstring;  
  81. this.c.SelectionStart = phoneinputstring.Length;  
  82. }  
  83. }  
  84. // if we are at the end of the data entry, then jump to the next field.
  85. if(formaterrorflag == 0 && phoneinputstring.Length == 11)  
  86. {  
  87. this.nexttextBox.Focus();  
  88. }  
  89. // if the user entered a period character, then blank  
  90. // what has been typed and make the user start over with  
  91. // something that will hopefully conform to the input
  92. // mask we are using.  
  93. if (formaterrorflag > 0 || e.KeyValue == 46)  
  94. {  
  95. this.PhoneNumber.Text.Remove(0, phoneinputstring.Length);  
  96. this.PhoneNumber.Text = "";  
  97. }  
  98. }  

Finally, here is an input mask for a currency amount. The fire_StartAmountEntered function is fired whenever the user types something into the currency_amount.Text text box. After leaving the currency_amount.Text text box, supplemental formatting is applied to compensate for omitted decimals and/or decimal places in the inputted data.

  1. // input mask formatting for the currency amount. this will trigger with each
  2. // successive keystroke as the user enters the currency amount digits.      
  3. privatevoid fire_StartAmountEntered(object sender, KeyEventArgs e)  
  4. {  
  5. int formaterrorflag;  
  6. string amountinputstring;  
  7. char[] amountcharacterarray;  
  8. // get the data from the “currency_amount.Text” text  
  9. // box and convert to character array for further processing.       
  10. amountinputstring = currency_amount.Text;  
  11. amountcharacterarray = amountinputstring.ToCharArray(0, amountinputstring.Length);  
  12. formaterrorflag = 0;  
  13. // next, compare each of the 10 chars to make sure they
  14. // conform to the currency amount input mask.   
  15. if(amountinputstring.Length >= 1)  
  16. {  
  17. if(amountcharacterarray[0] < (char)32 || amountcharacterarray[0] > (char)57) formaterrorflag = 1;  
  18. }  
  19. if(amountinputstring.Length >= 2)  
  20. {  
  21. if(amountcharacterarray[1] < (char)32 || amountcharacterarray[1] > (char)57) formaterrorflag = 1;  
  22. }  
  23. if(amountinputstring.Length >= 3)  
  24. {  
  25. if(amountcharacterarray[2] < (char)32 || amountcharacterarray[2] > (char)57) formaterrorflag = 1;  
  26. }  
  27. if(amountinputstring.Length >= 4)  
  28. {  
  29. if(amountcharacterarray[3] < (char)32 || amountcharacterarray[3] > (char)57) formaterrorflag = 1;  
  30. }  
  31. if(amountinputstring.Length == 5)  
  32. {  
  33. if(amountcharacterarray[4] < (char)32 || amountcharacterarray[4] > (char)57) formaterrorflag = 1;  
  34. }  
  35. if(amountinputstring.Length >= 6)  
  36. {  
  37. if(amountcharacterarray[5] < (char)32 || amountcharacterarray[5] > (char)57) formaterrorflag = 1;  
  38. }  
  39. if(amountinputstring.Length >= 7)  
  40. {  
  41. if(amountcharacterarray[6] < (char)32 || amountcharacterarray[6] > (char)57) formaterrorflag = 1;  
  42. }  
  43. if(amountinputstring.Length >= 8)  
  44. {  
  45. if(amountcharacterarray[7] < (char)32 || amountcharacterarray[7] > (char)57) formaterrorflag = 1;  
  46. }  
  47. if(amountinputstring.Length >= 9)  
  48. {  
  49. if(amountcharacterarray[8] < (char)32 || amountcharacterarray[8] > (char)57) formaterrorflag = 1;  
  50. }  
  51. if(amountinputstring.Length == 10)  
  52. {  
  53. if(amountcharacterarray[9] < (char)32 || amountcharacterarray[9] > (char)57) formaterrorflag = 1;  
  54. }  
  55. // if there is an error, then blank what has been typed and make the user
  56. // start over with something that will hopefully conform to the input
  57. // mask we are using.
  58. if(formaterrorflag > 0)  
  59. {  
  60. this.currency_amount.Text.Remove(0, amountinputstring.Length);  
  61. this.currency_amount.Text = "";  
  62. }  
  63. }  
  64. // this will format the entered currency amount after the
  65. // “currency_amount.Text” text box has lost focus. it will add a decimal
  66. // point and/or decimal places where needed.  
  67. privatevoidfire_LeaveAmountEntered(object sender, EventArgs e)  
  68. {  
  69. intflag_variable, counter;  
  70. longmanual_amount;  
  71. stringstramount_initial, stramount_finished;  
  72. // convert the “currency_amount.Text” text box to a string, then
  73. // begin the formatting process.
  74. stramount_initial = currency_amount.Text;  
  75. stramount_finished = currency_amount.Text;  
  76. if(stramount_initial.EndsWith("."))  
  77. {  
  78. stramount_finished = stramount_initial + "00";  
  79. }  
  80. // this concatenates one decimal place to the right of the data.
  81. if(stramount_initial.EndsWith(".0") || stramount_initial.EndsWith(".1") || stramount_initial.EndsWith(".2") || stramount_initial.EndsWith(".3") || stramount_initial.EndsWith(".4") || stramount_initial.EndsWith(".5") || stramount_initial.EndsWith(".6") || stramount_initial.EndsWith(".7") || stramount_initial.EndsWith(".8") || stramount_initial.EndsWith(".9"))  
  82. {  
  83. stramount_finished = stramount_initial + "0";  
  84. }  
  85. // this concatenates a decimal point with 2 decimal places to the
  86. // right of the data.
  87. if(!stramount_initial.Contains("."))  
  88. {  
  89. stramount_finished = stramount_initial + ".00";  
  90. }  
  91.   // this next block of code will left pad the currency
  92. // amount with a variable number of spaces depending
  93. // on the length in characters of the inputted data.    
  94. flag_variable = 0;  
  95. if (stramount_finished.Length == 1)  
  96. {  
  97. stramount_finished = "         " + stramount_finished;  
  98. flag_variable = 1;  
  99. }  
  100. if (stramount_finished.Length == 2 && a == 0)  
  101. {  
  102. stramount_finished = "        " + stramount_finished;  
  103. flag_variable = 1;  
  104. }  
  105. if (stramount_finished.Length == 3 && a == 0)  
  106. {  
  107. stramount_finished = "       " + stramount_finished;  
  108. flag_variable = 1;  
  109. }  
  110. if (stramount_finished.Length == 4 && a == 0)  
  111. {  
  112. stramount_finished = "      " + stramount_finished;  
  113. flag_variable = 1;  
  114. }  
  115. if (stramount_finished.Length == 5 && a == 0)  
  116. {  
  117. stramount_finished = "     " + stramount_finished;  
  118. flag_variable = 1;  
  119. }  
  120. if (stramount_finished.Length == 6 && a == 0)  
  121. {  
  122. stramount_finished = "    " + stramount_finished;  
  123. flag_variable = 1;  
  124. }  
  125. if (stramount_finished.Length == 7 && a == 0)  
  126. {  
  127. stramount_finished = "   " + stramount_finished;  
  128. flag_variable = 1;  
  129. }  
  130. if (stramount_finished.Length == 8 && a == 0)  
  131. {  
  132. stramount_finished = "  " + stramount_finished;  
  133. flag_variable = 1;  
  134. }  
  135. if (stramount_finished.Length == 9 && a == 0)  
  136. {  
  137. stramount_finished = " " + stramount_finished;  
  138. flag_variable = 1;  
  139. }  
  140. // convert the formatted currency amount to a long integer,
  141. // then use that as a guide for inserting commas in its
  142. // string expression counterpart.  
  143. for (counter = 0; counter < 10; counter++)  
  144. {  
  145. convert_to_number[a] = 0;  
  146. if (stramount_finished.Substring(counter, 1) == "0") convert_to_number[counter] = 0;  
  147. if (stramount_finished.Substring(counter, 1) == "1") convert_to_number[counter] = 1;  
  148. if (stramount_finished.Substring(counter, 1) == "2") convert_to_number[counter] = 2;  
  149. if (stramount_finished.Substring(counter, 1) == "3") convert_to_number[counter] = 3;  
  150. if (stramount_finished.Substring(counter, 1) == "4") convert_to_number[counter] = 4; counter  
  151. if (stramount_finished.Substring(counter, 1) == "5") convert_to_number[counter] = 5;  
  152. if (stramount_finished.Substring(counter, 1) == "6") convert_to_number[counter] = 6;  
  153. if (stramount_finished.Substring(counter, 1) == "7") convert_to_number[counter] = 7;  
  154. if (stramount_finished.Substring(counter, 1) == "8") convert_to_number[counter] = 8;  
  155. if (stramount_finished.Substring(counter, 1) == "9") convert_to_number[counter] = 9;  
  156. }  
  157. manual_amount = ((convert_to_number[0] * 100000000) + (convert_to_number[1] * 10000000) + (convert_to_number[2] * 1000000) + (convert_to_number[3] * 100000) + (convert_to_number[4] * 10000) + (convert_to_number[5] * 1000) + (convert_to_number[6] * 100) + (convert_to_number[8] * 10) + (convert_to_number[9] * 1));  
  158. // this will insert commas in the appropriate places as mentioned
  159. // above.  
  160. if (manual_amount >= 100000 && manual_amount < 100000000 && stramount_finished.Substring(3, 1) != " ")  
  161. {  
  162. stramount_finished = stramount_finished.Substring(0, 4) + "," + stramount_finished.Substring(4, 6);  
  163. }  
  164. if (manual_amount >= 100000000 && stramount_finished.Substring(0, 1) != " ")  
  165. {  
  166. stramount_finished = stramount_finished.Substring(0, 1) + "," + stramount_finished.Substring(1, 3) + "," + stramount_finished.Substring(4, 6);  
  167. }  
  168. if (manual_amount < 100000)  
  169. {  
  170. stramount_finished = " " + stramount_finished;  
  171. }  
  172. // assign this formatted currency amount back to the
  173. // “currency_amount.Text” text box.  
  174. this.currency_amount.Text = stramount_finished;  
  175. }  

These are typical examples of input masks I use for application development projects. And these are purely homegrown…I didn’t glean any of the custom software design from someone’s programming. They have proven themselves to be quite reliable, too.


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1257A1
Enter key:
Article Contributed By NE Ohio Computer Guy:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues. And don‘t forget to check out my YouTube Channel. It is full of cool videos and has something for everyone!

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #104918 Counter
151
Since 11/13/2020
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2021 Prestwood IT Solutions.   [Security & Privacy]