I have always had a need to create “input masks” for my C# data entry screens. They are used to format the data entered into 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. private void 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 mask we are using.
  92. if (formaterrorflag > 0 || e.KeyValue == 46)
  93. {
  94. this.PhoneNumber.Text.Remove(0, phoneinputstring.Length);
  95. this.PhoneNumber.Text = "";
  96. }
  97. }
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. private void 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 mask we are using.
  57. if (formaterrorflag > 0)
  58. {
  59. this.currency_amount.Text.Remove(0, amountinputstring.Length);
  60. this.currency_amount.Text = "";
  61. }
  62. }
  63. // this will format the entered currency amount after the “currency_amount.Text” text
  64. // box has lost focus. It will add a decimal point and/or decimal places where needed.
  65. private void fire_LeaveAmountEntered(object sender, EventArgs e)
  66. {
  67. int flag_variable, counter;
  68. long manual_amount;
  69. string stramount_initial, stramount_finished;
  70. // convert the “currency_amount.Text” text box to a string, then
  71. // begin the formatting process.
  72. stramount_initial = currency_amount.Text;
  73. stramount_finished = currency_amount.Text;
  74. if (stramount_initial.EndsWith("."))
  75. {
  76. stramount_finished = stramount_initial + "00";
  77. }
  78. // this concatenates one decimal place to the right of the data.
  79. 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"))
  80. {
  81. stramount_finished = stramount_initial + "0";
  82. }
  83. // this concatenates a decimal point with 2 decimal places to the right of the data.
  84. if (!stramount_initial.Contains("."))
  85. {
  86. stramount_finished = stramount_initial + ".00";
  87. }
  88. // this next block of code will left pad the currency amount with a
  89. // variable number of spaces depending on the length in characters
  90. // of the inputted data.
  91. flag_variable = 0;
  92. if (stramount_finished.Length == 1)
  93. {
  94. stramount_finished = " " + stramount_finished;
  95. flag_variable = 1;
  96. }
  97. if (stramount_finished.Length == 2 && a == 0)
  98. {
  99. stramount_finished = " " + stramount_finished;
  100. flag_variable = 1;
  101. }
  102. if (stramount_finished.Length == 3 && a == 0)
  103. {
  104. stramount_finished = " " + stramount_finished;
  105. flag_variable = 1;
  106. }
  107. if (stramount_finished.Length == 4 && a == 0)
  108. {
  109. stramount_finished = " " + stramount_finished;
  110. flag_variable = 1;
  111. }
  112. if (stramount_finished.Length == 5 && a == 0)
  113. {
  114. stramount_finished = " " + stramount_finished;
  115. flag_variable = 1;
  116. }
  117. if (stramount_finished.Length == 6 && a == 0)
  118. {
  119. stramount_finished = " " + stramount_finished;
  120. flag_variable = 1;
  121. }
  122. if (stramount_finished.Length == 7 && a == 0)
  123. {
  124. stramount_finished = " " + stramount_finished;
  125. flag_variable = 1;
  126. }
  127. if (stramount_finished.Length == 8 && a == 0)
  128. {
  129. stramount_finished = " " + stramount_finished;
  130. flag_variable = 1;
  131. }
  132. if (stramount_finished.Length == 9 && a == 0)
  133. {
  134. stramount_finished = " " + stramount_finished;
  135. flag_variable = 1;
  136. }
  137. // convert the formatted currency amount to a long integer, then use that as a guide for
  138. // inserting commas in its string expression counterpart.
  139. for (counter = 0; counter < 10; counter++)
  140. {
  141. convert_to_number[a] = 0;
  142. if (stramount_finished.Substring(counter, 1) == "0") convert_to_number[counter] = 0;
  143. if (stramount_finished.Substring(counter, 1) == "1") convert_to_number[counter] = 1;
  144. if (stramount_finished.Substring(counter, 1) == "2") convert_to_number[counter] = 2;
  145. if (stramount_finished.Substring(counter, 1) == "3") convert_to_number[counter] = 3;
  146. if (stramount_finished.Substring(counter, 1) == "4") convert_to_number[counter] = 4; counter
  147. if (stramount_finished.Substring(counter, 1) == "5") convert_to_number[counter] = 5;
  148. if (stramount_finished.Substring(counter, 1) == "6") convert_to_number[counter] = 6;
  149. if (stramount_finished.Substring(counter, 1) == "7") convert_to_number[counter] = 7;
  150. if (stramount_finished.Substring(counter, 1) == "8") convert_to_number[counter] = 8;
  151. if (stramount_finished.Substring(counter, 1) == "9") convert_to_number[counter] = 9;
  152. }
  153. 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));
  154. // this will insert commas in the appropriate places as mentioned above.
  155. if (manual_amount >= 100000 && manual_amount < 100000000 && stramount_finished.Substring(3, 1) != " ")
  156. {
  157. stramount_finished = stramount_finished.Substring(0, 4) + "," + stramount_finished.Substring(4, 6);
  158. }
  159. if (manual_amount >= 100000000 && stramount_finished.Substring(0, 1) != " ")
  160. {
  161. stramount_finished = stramount_finished.Substring(0, 1) + "," + stramount_finished.Substring(1, 3) + "," + stramount_finished.Substring(4, 6);
  162. }
  163. if (manual_amount < 100000)
  164. {
  165. stramount_finished = " " + stramount_finished;
  166. }
  167. // assign this formatted currency amount back to the “currency_amount.Text” text box.
  168. this.currency_amount.Text = stramount_finished;
  169. }
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 code from someone’s programming. They have proven themselves to be quite reliable, too.