Solved

TextBox Date format

Posted on 2004-03-30
7
906 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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