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.
-
-
- private void fire_DateEntered(object sender, KeyEventArgs e)
- {
- int formaterrorflag;
- string dateinputstring;
- char[] datecharacterarray;
-
-
-
- dateinputstring = TransactionDate.Text;
- datecharacterarray = dateinputstring.ToCharArray(0, dateinputstring.Length);
-
- formaterrorflag = 0;
-
-
-
- if (e.KeyValue != 8 && e.KeyValue != 46)
- {
- if (dateinputstring.Length >= 1)
- {
- if ((datecharacterarray[0] < (char)48 || datecharacterarray[0] > (char)49) && datecharacterarray[0] > (char)32) formaterrorflag = 1;
- }
- if (dateinputstring.Length >= 2)
- {
- if ((datecharacterarray[1] < (char)48 || datecharacterarray[1] > (char)57) && datecharacterarray[1] > (char)32) formaterrorflag = 2;
- }
- if (dateinputstring.Length >= 3)
- {
- if (datecharacterarray[2] != (char)47) formaterrorflag = 22;
- }
- if (dateinputstring.Length >= 4)
- {
- if ((datecharacterarray[3] < (char)48 || datecharacterarray[3] > (char)51) && datecharacterarray[3] > (char)32) formaterrorflag = 3;
- }
- if (dateinputstring.Length >= 5)
- {
- if ((datecharacterarray[4] < (char)48 || datecharacterarray[4] > (char)57) && datecharacterarray[4] > (char)32) formaterrorflag = 4;
- }
- if (dateinputstring.Length >= 6)
- {
- if (datecharacterarray[5] != (char)47) formaterrorflag = 55;
- }
- if (dateinputstring.Length >= 7)
- {
- if ((datecharacterarray[6] < (char)48 || datecharacterarray[6] > (char)57) && datecharacterarray[6] > (char)32) formaterrorflag = 5;
- }
- if (dateinputstring.Length == 8)
- {
- if ((datecharacterarray[7] < (char)48 || datecharacterarray[7] > (char)57) && datecharacterarray[7] > (char)32) formaterrorflag = 6;
- }
-
- }
-
-
-
-
- if (formaterrorflag == 0)
- {
- if (dateinputstring.Length == 2)
- {
- dateinputstring = dateinputstring + "/";
- this.TransactionDate.Text = dateinputstring;
- this.TransactionDate.SelectionStart = dateinputstring.Length;
- }
- if (dateinputstring.Length == 5)
- {
- dateinputstring = dateinputstring + "/";
- this.TransactionDate.Text = dateinputstring;
- this.TransactionDate.SelectionStart = dateinputstring.Length;
- }
- }
-
-
- if (formaterrorflag == 0 && dateinputstring.Length == 7)
- {
- this.nexttextBox.Focus();
- }
-
-
-
-
- if (formaterrorflag > 0 || e.KeyValue == 46)
- {
- this.TransactionDate.Text.Remove(0, dateinputstring.Length);
- this.TransactionDate.Text = "";
- }
-
- }
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.
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.
-
-
- private void fire_StartAmountEntered(object sender, KeyEventArgs e)
- {
- int formaterrorflag;
- string amountinputstring;
- char[] amountcharacterarray;
-
-
-
- amountinputstring = currency_amount.Text;
- amountcharacterarray = amountinputstring.ToCharArray(0, amountinputstring.Length);
-
- formaterrorflag = 0;
-
-
-
- if (amountinputstring.Length >= 1)
- {
- if (amountcharacterarray[0] < (char)32 || amountcharacterarray[0] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 2)
- {
- if (amountcharacterarray[1] < (char)32 || amountcharacterarray[1] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 3)
- {
- if (amountcharacterarray[2] < (char)32 || amountcharacterarray[2] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 4)
- {
- if (amountcharacterarray[3] < (char)32 || amountcharacterarray[3] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length == 5)
- {
- if (amountcharacterarray[4] < (char)32 || amountcharacterarray[4] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 6)
- {
- if (amountcharacterarray[5] < (char)32 || amountcharacterarray[5] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 7)
- {
- if (amountcharacterarray[6] < (char)32 || amountcharacterarray[6] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 8)
- {
- if (amountcharacterarray[7] < (char)32 || amountcharacterarray[7] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length >= 9)
- {
- if (amountcharacterarray[8] < (char)32 || amountcharacterarray[8] > (char)57) formaterrorflag = 1;
- }
- if (amountinputstring.Length == 10)
- {
- if (amountcharacterarray[9] < (char)32 || amountcharacterarray[9] > (char)57) formaterrorflag = 1;
- }
-
-
-
- if (formaterrorflag > 0)
- {
- this.currency_amount.Text.Remove(0, amountinputstring.Length);
- this.currency_amount.Text = "";
- }
-
- }
-
-
-
- private void fire_LeaveAmountEntered(object sender, EventArgs e)
- {
- int flag_variable, counter;
- long manual_amount;
- string stramount_initial, stramount_finished;
-
-
-
-
- stramount_initial = currency_amount.Text;
- stramount_finished = currency_amount.Text;
-
- if (stramount_initial.EndsWith("."))
- {
- stramount_finished = stramount_initial + "00";
- }
-
-
- 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"))
- {
- stramount_finished = stramount_initial + "0";
- }
-
-
- if (!stramount_initial.Contains("."))
- {
-
- stramount_finished = stramount_initial + ".00";
- }
-
-
-
-
-
- flag_variable = 0;
- if (stramount_finished.Length == 1)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 2 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 3 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 4 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 5 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 6 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 7 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 8 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
- if (stramount_finished.Length == 9 && a == 0)
- {
- stramount_finished = " " + stramount_finished;
- flag_variable = 1;
- }
-
-
-
-
- for (counter = 0; counter < 10; counter++)
- {
- convert_to_number[a] = 0;
- if (stramount_finished.Substring(counter, 1) == "0") convert_to_number[counter] = 0;
- if (stramount_finished.Substring(counter, 1) == "1") convert_to_number[counter] = 1;
- if (stramount_finished.Substring(counter, 1) == "2") convert_to_number[counter] = 2;
- if (stramount_finished.Substring(counter, 1) == "3") convert_to_number[counter] = 3;
- if (stramount_finished.Substring(counter, 1) == "4") convert_to_number[counter] = 4; counter
- if (stramount_finished.Substring(counter, 1) == "5") convert_to_number[counter] = 5;
- if (stramount_finished.Substring(counter, 1) == "6") convert_to_number[counter] = 6;
- if (stramount_finished.Substring(counter, 1) == "7") convert_to_number[counter] = 7;
- if (stramount_finished.Substring(counter, 1) == "8") convert_to_number[counter] = 8;
- if (stramount_finished.Substring(counter, 1) == "9") convert_to_number[counter] = 9;
- }
- 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));
-
-
-
- if (manual_amount >= 100000 && manual_amount < 100000000 && stramount_finished.Substring(3, 1) != " ")
- {
- stramount_finished = stramount_finished.Substring(0, 4) + "," + stramount_finished.Substring(4, 6);
- }
- if (manual_amount >= 100000000 && stramount_finished.Substring(0, 1) != " ")
- {
- stramount_finished = stramount_finished.Substring(0, 1) + "," + stramount_finished.Substring(1, 3) + "," + stramount_finished.Substring(4, 6);
- }
- if (manual_amount < 100000)
- {
- stramount_finished = " " + stramount_finished;
- }
-
-
- this.currency_amount.Text = stramount_finished;
-
-
- }
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.