Click here to Skip to main content
15,868,419 members
Articles / Programming Languages / Visual Basic 10

Text Box for Currency in VB.NET 2010

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Jul 2010GPL34 min read 82.2K   3.8K   6   13
This component will be used as a text box for automatically formatted currency values.

Introduction

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.

currencytextbox1.PNG currencytextbox2.PNG

Background

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:

  1. Download the code. Then unzip it.
  2. 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.
  3. Then in your solution or project, add the component project using the Add an Existing Project.
  4. 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 46.

VB.NET
Const LOCATIONCODE As String = "en-PH"
Const DECIMALPOINT As Integer = 46
Const CURRFORM As String = "C"

In the 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 46 to 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:

VB.NET
Dim myDecimal as Decimal = 9876543.21
CurrencyTextBox1.Text = myDecimal ' Assigning a decimal (strict=off) 

or:

VB.NET
CurrencyTextBox1.Text = "9876543.21" ' Assigning a text or an integer      
CurrencyTextBox1.Text = MyInteger  ' Assigning an integer (strict=off) 

Assigning a 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.

VB.NET
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.

History

  • 12th July, 2010: Initial post

I'll keep you updated when I find certain bugs.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer Kinalibangon Software
Philippines Philippines
Electronics and Communications Engineering student at our local university and a part time software developer for Kinalibangon Software.

Comments and Discussions

 
QuestionNegative numbers Pin
Member 1300496814-Sep-18 2:48
Member 1300496814-Sep-18 2:48 
AnswerRe: Negative numbers [RESOLVED] Pin
Member 1300496817-Sep-18 4:33
Member 1300496817-Sep-18 4:33 
QuestionConvert code to c# Pin
not!@me;just@smile29-Apr-17 1:27
not!@me;just@smile29-Apr-17 1:27 
Questionthanks Pin
Khernie Anaque28-Feb-13 20:11
Khernie Anaque28-Feb-13 20:11 
AnswerRe: thanks Pin
Bangon Kali28-Feb-13 23:24
Bangon Kali28-Feb-13 23:24 
Questionany can hepl me??? Pin
herman15bamaco11-Nov-11 14:35
herman15bamaco11-Nov-11 14:35 
AnswerRe: any can hepl me??? Pin
Bangon Kali11-Nov-11 23:41
Bangon Kali11-Nov-11 23:41 
GeneralRe: any can hepl me??? Pin
herman15bamaco13-Nov-11 18:52
herman15bamaco13-Nov-11 18:52 
Generalthanks Pin
waaal moooh22-Feb-11 4:01
waaal moooh22-Feb-11 4:01 
GeneralMy vote of 5 Pin
Member 39004769-Oct-10 21:31
Member 39004769-Oct-10 21:31 
GeneralExplanation required Pin
Md. Marufuzzaman12-Jul-10 1:47
professionalMd. Marufuzzaman12-Jul-10 1:47 
GeneralRe: Explanation required Pin
Bangon Kali12-Jul-10 5:35
Bangon Kali12-Jul-10 5:35 
GeneralRe: Explanation required Pin
Md. Marufuzzaman12-Jul-10 5:48
professionalMd. Marufuzzaman12-Jul-10 5:48 

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.