Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A numeric keypad for payment amount entry in C#

0.00/5 (No votes)
14 May 2017 1  
This numeric keypad helps you enter payment amounts in a text field

Introduction

For end users, it is much easier if they could just enter the digits from a numeric keypad and the currency format automatically applies, instead of entering a decimal point and digits!

Background

In many PoS software applications (or similar environments) a payment screen requires a "payment amount entry". Payment values (currency) almost always include a decimal point. So, if we don't handle the decimal point and leave it to the user it could (it will !) become a point of failure, when user enters more than one decimal point or simply adds the decimal point where it changes the total amount (A $10.25 payments becomes $102.50) So, you either have to check the value, or catch it at keypress level. A better method is to manage it at the entry point!

 

Using the code

On a Windows form add buttons and build your "Price numeric keypad":

Payment Coders - Price numeric kaypad

 

 

 

 

 

 

 

 

 

 

 

I have used a TableLayoutPanel of 4x4.

Add a Textbox under the keypad grid, where the output will be placed.

I have introduced a public string, which decides what role our keypad will be playing:

public string numberRole = "MONEY" ;

"Money" role will set the satge for a payment value where a two digits decimal will be applied. For instance if the amount is 10.25, we will have the followings:

step 1: 0.01

step 2: 0.10

step 3: 1.02

step 4: 10.25

"DIGITS" role will simply turn our keypad into a numeric, without decimal point.

I also control the number of decimal points and also decimal digits(preset is 2).

private void MakeAmount(string amountTxt)

Is the key player in this code.

 

private void MakeAmount(string amountTxt)
    {
        decimal meanWhile;
        txtValue.Text = txtValue.Text + amountTxt;
        if (txtValue.Text.Length == 1)
        {
            txtValue.Text = "0.0" + txtValue.Text;
        }
        if (txtValue.Text.Length > 4)
        {
            meanWhile = Convert.ToDecimal(txtValue.Text) * 10;
            txtValue.Text = meanWhile.ToString("G29");
        }
    }

 

As you can see depends on what we have, the decimal points gets moved.

 

For every digit we have the same process:

 

Except for zero, which we need to check a few things, all other Click events are similar:

private void num2_Click(object sender, EventArgs e) { if (numberRole == "DIGITS") { txtValue.Text = txtValue.Text + "2" ; } else MakeAmount(num2.Text); }

And for zero:

private void num0_Click(object sender, EventArgs e)
       {
           decimal meanWhile;
           if (numberRole == "DIGITS")
           {
               txtValue.Text = txtValue.Text + "0";
           }
           else
           {
               txtValue.Text = txtValue.Text + "0";
               if (txtValue.Text.Length == 1)
               {
                   txtValue.Text = "0.0" + txtValue.Text;
               }
               if (txtValue.Text.Length >= 4)
               {
                   meanWhile = Convert.ToDecimal(txtValue.Text) * 10;
                   txtValue.Text = meanWhile.ToString("G29");
               }
               if (Convert.ToDecimal(txtValue.Text) < 1)
               {
                   txtValue.Text = txtValue.Text + "0";
               }
               if (txtValue.Text.IndexOf(".") < 0)
               {
                   txtValue.Text = txtValue.Text + ".00";
               }
               if (Convert.ToDecimal(txtValue.Text) > 1 && (txtValue.Text.Length - txtValue.Text.IndexOf(".") - 1 == 1))
               {
                   txtValue.Text = txtValue.Text + "0";
               }
           }
       }

This is how you call your price numeric keypad:

numberpad showNumber = new numberpad(); showNumber.numberRole = "MONEY";

As you can see this calls for a "Money" format.

And if you need a numeric keypad, call your function with "DIGITS":

numberpad showNumber = new numberpad(); showNumber.numberRole = "DIGITS";

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here