Click here to Skip to main content
Licence CPOL
First Posted 20 Nov 2007
Views 18,054
Downloads 392
Bookmarked 19 times

Currency Input in VB.NET

By | 29 Mar 2009 | Article
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)

About the Author

Jon Law

Software Developer
J.L. Productions
Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralScreenshot would be interesting too... PinmemberJohnny J.21:09 29 Mar '09  
GeneralSample would be great. Pinmembercrampio14:31 23 Oct '08  
GeneralRe: Sample would be great. PinmemberJon Law14:39 23 Oct '08  

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.

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