Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie - textbox - validating to currency or decimal? 2

Status
Not open for further replies.

honeypot

Technical User
Mar 6, 2001
147
GB
hi there! i have a windows form that will enable users to (hopefully) enter details to update a database. One of the textboxes will be used to update a currency or decimal value. How do i set this? Or validate it so that they can only enter say for example "102.00" or "34.58"??
 
Honeypot,

I would go step through the input when they leave the textbox and make sure it is in the proper format.

ex:

sub textbox_leave

tempholder = textbox.text
while count < textbox.textlength
tempchar = tempholder.substring(count,1)
select case tempchar
case &quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;0&quot;
'do what you need to do
case &quot;.&quot;
afterdec = tempholder.substring(count+1)
if afterdec.length = 2
count = textbox.textlength + 1
properformat = true
return
else
count = textbox.textlength + 1
properformat = false
return
case else
properformat = false
return
end select

'tell user that the input was not valid

Hope this helps,

Brian
 
Along with Ragnarox's way of thinking, it would be advisable to subclass the base textbox, into a control that is only used for entering numbers. This way you only have to have the code once, and you can assign properties for say overall length and number of decimals allowed.





Sweep
...if it works dont mess with it
 
You could also use the built in parsing for decimals:
Code:
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

    Try

        Decimal.Parse(TextBox1.Text, Globalization.NumberStyles.Currency)

    Catch

        MessageBox.Show(&quot;Not a currency or decimal value.&quot;)

        e.Cancel = True

    End Try

End Sub
 
Thanks everyone for your help :) will give it a go now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top