Solved

TextBox Date format

Posted on 2004-03-30
7
903 Views
Last Modified: 2008-02-01
Hello, I have formatted the value held in a textbox to type of currency.    
                 
      Format (me.txtTotal.Text, "currency")

I have various textboxes on the form each with similar currency value in them.  I want to be able to add up all the values in the textbox, e.g £23.50 + £5.34,  the problem is that the results that gets returned is zero (0),  because it (must then be) a string and not a numerical type datatype.

Is there a property that i can set that changes the default display of the textbox to a currency type but allows me to add up the values without having to pass then through a function to get values.  This can be done in VB 6 and Access Forms, is there a setting on the textbox property that would help me do this?

Thanks
0
Comment
Question by:sublimation
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 10714329
cdbl(txtwhatever.text)
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 10714340
You ca also use the InputMask

Of the MaskedEditControl and right click and goto properties and set the format and then you just do
Mask.Value
0
 
LVL 15

Expert Comment

by:SRigney
ID: 10714981
VB6 and Access also call the function to convert the string to a number, it just does it behind the scenes without you having to add the code for that.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Author Comment

by:sublimation
ID: 10716193
I forgot to mention that i am using vb.net.  I cant seem to see the maskededitcontrol in vb.net
0
 
LVL 14

Expert Comment

by:ptakja
ID: 10716221
You can also try Double.Parse(txtbox.Text) which will return the value as a Double.

So, something like this:

Dim dblTotal as Double = Double.Parse(Textbox1.Text) + Double.Parse(TextBox2.Text)  + etc...

OR, perhaps a little more elegant:

Dim dboTotal as Double
' Copy TextBoxes in an array
Dim txtBoxes as TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, Textbox5}

' Loop through array adding up the values.
For ii as Integer = 0 To txtBoxes.Count -1
    dblTotal += Double.Parse(txtBoxes(ii))
Next



Jeff
0
 
LVL 4

Author Comment

by:sublimation
ID: 10723752
Hello, none of the above work since the values in the textboxes are strings, like "£34.25".
   
I want to have the value in the textbox as a currency, e.g £45.76, but then be able to add them up.  

Is there a way that a textbox will display any number entered into it as a currency and let you add up the values.

I am using VB.Net
0
 
LVL 14

Accepted Solution

by:
ptakja earned 250 total points
ID: 10724153
Got it.  Try this:

Format(CType(Decimal.Parse(TextBox1.Text, Globalization.NumberStyles.Currency) + Decimal.Parse(TextBox2.Text, Globalization.NumberStyles.Currency), String), "Currency")

My sample code in previous post rewritten as shown above:

Dim dblTotal as Decimal = Decimal.Parse(Textbox1.Text, Globalization.NumberStyles.Currency) + Double.Parse(TextBox2.Text, Globalization.NumberStyles.Currency)  + etc...

OR, perhaps a little more elegant:

Dim dboTotal as Double
' Copy TextBoxes in an array
Dim txtBoxes as TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, Textbox5}

' Loop through array adding up the values.
For ii as Integer = 0 To txtBoxes.Count -1
    dblTotal += Double.Parse(txtBoxes(ii), Globalization.NumberStyles.Currency)
Next

txtTotal.Text = Format(dbl.Total.ToString(), "Currency")
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question