Click here to Skip to main content
11,705,453 members (50,371 online)
Click here to Skip to main content

Currency Input in VB.NET

, 29 Mar 2009 CPOL 24.4K 871 20
Rate this:
Please Sign up or sign in to vote.
A Currency Input Box in VB.NET

Introduction

While working on a rather large project, I came across the need to input currency values quite frequently. Due to the extremely high level of standards for this project, using masked text boxes just wasn't acceptable. This box is very easy to use, simply reference the library and drop the control onto your form. The code in this article is from the VB.NET version since that's the version I created first.

After this article had been up for a day, the response I was getting was very under-whelming so I spent the day cleaning up the code and expanding the article itself. Hopefully this new version is more along the lines you're used to seeing on this site.

The ZIP file contains the full solution. The DLL is in the Currency Library\bin\release folder.

Background

The box is in a control library and inherits a normal text box with a property to get information in and out, and overridden KeyPress and KeyDown events. This version does NOT handle thousand separators but that is coming and I will post the new version as soon as it is ready.

Using the Code

Using this box couldn't be easier; reference the appropriate library and add it as you would any other control. The values passed in and out are of the Currency class. This class is also defined in the DLL and includes methods to set values from Single, Double, Decimal, or String values. It also has constructors for each of those types and operators for addition, subtraction, multiplication, sign inversion, and taxes. The source code is well documented, including XML tags, so I won't go too much into it here. Most of it is nearly endlessly nested in If statements.

Points of Interest

I was amazed at how many checks needed to be done to keep the value valid at all times. In time, I think I've come up with a rather useful control that's been absent from VB for a long time.

Major Change

Since the last version was posted, I discovered a major flaw in dealing with the delete and backspace keys. I was using e.Handled to indicate that they had been dealt with but was still getting two key presses for the price of one. In correcting this problem, I discovered e.SupressKeyPress which corrects the problem nicely. I have also moved where the e.Handled and e.SupressKeyPress calls are so that no "bad" characters are added and the other functionality of the text box isn't lost.

I have also discovered that the changes made in the textbox were not being reflected in the underlying Currency value. This has been fixed.

I’ve also made it easier to get and set the Currency value for the textbox by making the Amount property read/write.

History

  • v1.0 - The initial version implements everything except thousands separators and negative values
  • v1.1 - Cleaned up the source code by removing some redundant checks and making it easier to follow
  • v2.1 - Added the Currency class, negative value support, and fixed delete and backspace handling
  • v2.2 – Fixed a problem where changes in the textbox weren't being updated to the underlying Currency value

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jon Law
Software Developer J.L. Productions
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralScreenshot would be interesting too... Pin
Johnny J.29-Mar-09 21:09
memberJohnny J.29-Mar-09 21:09 
GeneralSample would be great. Pin
crampio23-Oct-08 14:31
membercrampio23-Oct-08 14:31 
GeneralRe: Sample would be great. Pin
Jon Law23-Oct-08 14:39
memberJon Law23-Oct-08 14:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 29 Mar 2009
Article Copyright 2007 by Jon Law
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid