I was making an accounting software when I realized using the stock Text Box component was poor for currency, hence I decided to develop one that automatically converts Decimal typed values to for example like "
PhP987,654,321.09" on text boxes. And I should also say this was made using Visual Basic 2010. However you can just copy the important file CurrencyTextBox.vb to any other version of Visual Basic and recompile it as a component class.
You'll need simple knowledge on using new components on your projects. Simply adding this project to your existing solution will do. Then if this new component doesn't show up in the toolbox on your own project, you might need to reference this project, then build it again after referencing. This requires that you have an existing solution and your own Windows Forms project in that solution since this only works with Windows Forms or others that can let you add components.
Using the Code
Here are simple steps to use the component in your own solutions or projects:
- Download the code. Then unzip it.
- You'll find inside the unzipped folder a solution file named "CurrencyTextBox.sln" and another folder called CurrencyTextBox. Go to that folder and you'll find several other folders and files. You should copy all of them to your solution's folder.
- Then in your solution or project, add the component project using the Add an Existing Project.
- Your project and this component's project are now in one solution. Rebuild the component's (
CurrencyTextBox) project so that it will show up in all of your other projects that need GUI Components in the toolbox.
Next, I'll explain more on how the code is made. In the code listing that follows, be sure to change the values of these constants to your preference. As you can see, it is set to "
en-PH", and the
DECIMALPOINT is set to
Const LOCATIONCODE As String = "en-PH"
Const DECIMALPOINT As Integer = 46
Const CURRFORM As String = "C"
LOCATIONCODE, it is referring to the sign of your currency. In its current settings, the decimal value that'll display in the text box will be something like this:
Php123.00. If you want to change it to dollar, use "
en-US" and if you want a complete list of standard values refer to this link from MSDN that talks about the Culture Info Class.
Next is the
DECIMALPOINT option. You should change it according to your preference. Some use this format in presenting decimal numbers: "
789.456,12". They use '
,' as the decimal point and '
.' as a groups of three separator for presenting numbers. This component on the contrary was initially set using this format: "
789,456.25", using '
,' as the groups of three separator and '
.' as the decimal point. Anyway, if you want to change it to the format show first ("
789.456,12"), you should change
44. Those are just the ASCII values of '
.' and '
EDIT: I almost forgot!
CURRFORM, you have to assign "C" to that. Since we are using currency as a format. More information about this is at this link.
Lastly, in using the component, you need to be aware of how to assign text and retrieve values from it. Assigning decimal values to the
textbox will be in the following manner:
Dim myDecimal as Decimal = 9876543.21
CurrencyTextBox1.Text = myDecimal
CurrencyTextBox1.Text = "9876543.21"
CurrencyTextBox1.Text = MyInteger
string that cannot be converted to decimal will result to an error. In the examples above, you can only assign
strings when your option strict is on in the project options. And in order to get the value of the currency text box, use the readonly value property.
Dim MyDecimal As Decimal
MyDecimal = CurrencyTextBox1.Value
The statement will return a decimal.
Points of Interest
Most part of the code was found on the internet. One particular part is very important which I found at this link, however this one only supports decimals, and requires a lot of modifications to work on currencies, however my work paid off since I'm currently using it on an accounting software. And I should be thankful, my client liked it very much compared to the previous version they've been using.
- 12th July, 2010: Initial post
I'll keep you updated when I find certain bugs.