Click here to Skip to main content
15,888,984 members
Articles / Programming Languages / Visual Basic

Currency Input in VB.NET

Rate me:
Please Sign up or sign in to vote.
2.74/5 (4 votes)
29 Mar 2009CPOL3 min read 39K   1.2K   20   3
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)


Written By
Software Developer J.L. Productions
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralScreenshot would be interesting too... Pin
Johnny J.29-Mar-09 21:09
professionalJohnny J.29-Mar-09 21:09 
GeneralSample would be great. Pin
crampio23-Oct-08 14:31
crampio23-Oct-08 14:31 
GeneralRe: Sample would be great. Pin
Jon Law23-Oct-08 14:39
Jon Law23-Oct-08 14:39 
CurrencyBoxAnswer.Value = CurrencyBox1.Value + CurrencyBox2.Value
Simple as that.

The other operators work just the same

CurrencyBoxAnswer.Value = CurrencyBox1.Value & 15 will add 15% to the value of CurrencyBox1 etc

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.