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