Click here to Skip to main content
15,890,670 members
Articles / Desktop Programming / Windows Forms

A numeric keypad for payment amount entry in C#

Rate me:
Please Sign up or sign in to vote.
3.71/5 (5 votes)
14 May 2017CPOL3 min read 19.9K   1.3K   1   4
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
United States United States
I sold my beloved racing bicycle to buy my first computer, a Sinclair home computer! That was over 30 years ago and I am still in love with what became my profession, and quite honestly my calling! I have received my BS and MS in Mathematics and Computer science and have been working in so many fields of software development, system architecture, and design and I have most enjoyed teaching and writing about programming languages and fiddling with new technologies!
I believe life is too short to ignore learning a programming language!

Comments and Discussions

 
GeneralLack of object-orientation Pin
John Brett15-May-17 2:07
John Brett15-May-17 2:07 
The article doesn't really make use of of any datatype other than string.

This kind of construct:-
if (numberRole == "DIGITS"){} else {}
is not an object-oriented design.
An Enum would be a good first step.
A better design would be one in which we have different implementations of the handler interface for each role (IRoleHandler, DigitsRoleHandler, NumberRoleHandler &c).

All of the processing is done at a textual level, rather than treating the data as numeric (which would appear, after all, to be the point of the article).
Richard's version demonstrates how much simpler the code becomes with that approach.

Whilst I'll accept that the code is likely correct, the text handling approach has a large number of edge cases, and there are no tests presented with the article (indeed the code is embedded within separate event handlers and would be hard to write tests for).

John
QuestionSimpler method PinPopular
Richard MacCutchan14-May-17 3:04
mveRichard MacCutchan14-May-17 3:04 
AnswerRe: Simpler method Pin
Kourosh K Tari15-May-17 7:32
Kourosh K Tari15-May-17 7:32 
GeneralRe: Simpler method Pin
Richard MacCutchan15-May-17 7:42
mveRichard MacCutchan15-May-17 7:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.