|
-
Apr 16th, 2012, 07:47 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Format as currency
Please excuse a beginner in VB Net with what is probably a really dumb question.
I am trying to write some code that will format the text in a text-box as currency, so that if the user enters "6", the text-box will show "$6.00" after the user exits the text-box. (I want to allow for all the variations of users entering 6 or 6.00 or $6 or $6.00.)
I've got:
HTML Code:
Private Sub txt_nett_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nett.TextChanged
txt_nett = FormatCurrency(txt_nett)
End Sub
and an error message that says: "Value of type 'String' cannot be converted to 'System.Windows.Forms.TextBox'.
The frustrating thing is that I could do this in a trice in VBA (with which I am much more familiar)!
Thank you
-
Apr 16th, 2012, 07:55 AM
#2
Re: Format as currency
txt_nett is a textbox. How can you format a textbox? Perhaps you need the text in this textbox... if so, then use its txt_nett.Text property.
VBA and VB6 permit such sloppiness and try to 'think for/instead of the coder' VB.Net is more strict and does not take actions on its own. The compiler does EXACTLY what's been told in the code. And you're trying to use an object that points to a usercontrol like plain text.
Last edited by cicatrix; Apr 16th, 2012 at 07:58 AM.
-
Apr 16th, 2012, 07:57 AM
#3
Hyperactive Member
Re: Format as currency
Your error doesn't have to do with the FormatCurrency function. The problem is something that a lot of VBA / VB 6.0 crossovers fall into... In VB.Net, the Text property of controls is no longer the default property. So you have to address this property specifically. Namely:
Code:
Private Sub txt_nett_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nett.TextChanged
txt_nett.Text = FormatCurrency(txt_nett.Text)
End Sub
-
Apr 16th, 2012, 08:07 AM
#4
Thread Starter
Addicted Member
Re: Format as currency
Thank you both. I'll need to fiddle a bit more, as the TextChanged event (which is the default VB.Net provides) isn't quite what I want. I need something like AfterUpdate or Exit.
-
Apr 16th, 2012, 08:09 AM
#5
Re: Format as currency
I'm curious to why he's using the FormatCurrency() function in the first place.
If you have a numeric variable (he would need to parse the current text in the TB to a numeric variable & if it passes convert it to currency) you can display it as currency using .ToString("c")
Here's an example:
vb Code:
Private Sub txt_nett_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nett.TextChanged
Dim value As Decimal
If Decimal.TryParse(txt_nett.Text.Trim(), value) Then
txt_nett.Text = value.ToString("c")
End If
End Sub
-
Apr 16th, 2012, 08:15 AM
#6
Thread Starter
Addicted Member
Re: Format as currency
The "Leave" event does the trick. Thank you all.
Last edited by Resource Dragon; Apr 16th, 2012 at 08:24 AM.
Reason: Changed "both" to "all" as third person replied as I was replying.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|