Home | Forums | Contact | Search | Syndication  
 
 [login] [create account]   Saturday, June 7, 2025 
 
slxdeveloper.com Community Forums  
   
The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
 Architect Forums - Controls
Forum to discuss usage & tips for SalesLogix controls and other 3rd party ActiveX controls. View the code of conduct for posting guidelines.
Forums RSS Feed


 Back to Forum List | Back to Controls | New ThreadView:  Search:  
 Author  Thread: Format float textbox to two decimal places
Steve Knowles
Posts: 657
Top 10 forum poster: 657 posts
 
Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 05 Apr 07 1:11 PM
fiogf49gjkf0d
I want to limit a textbox bound to a field with datatype=float to two decimal places. Currently FormatType=ftFixed and FormatString=%.2f - What is happening now is that you can enter more than two decimal places. When focus is removed the field rounds and displays two decimal places. When focus is put back in the field the actual number of unrounded decimal places are displayed again and this is what is written to the DB. How can I truely limit this to two decimal places?

Thanks
[Reply][Quote]
Jeff Weight
Posts: 219
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 06 Apr 07 10:41 AM
fiogf49gjkf0d
The key to this is the OnKeyPress event for the textbox. The format type stuff is just for appearances when the cursor isn't in the textbox, as you have seen - it doesn't really affect the data at all. I like to use the OnKeyPress event most often for making sure people only enter numbers or the "x" character into phone number fields. This is the function I use:

Sub PhoneKeyPress(Sender, Key)
If Not IsNumeric(Chr(Key)) And Chr(Key) <> "x" And Chr(Key) <> "X" And Key <> 8 Then Key = 0
End Sub

The function first checks to see if the key pressed is not numeric, and if it isn't an "x", and if the backspace key wasn't pressed (the Key = 8 if it was pressed), and if all those things are indeed the case, it changes the key to 0, which tells the computer "false alarm - nothing was pressed". It is that false alarm ability that you can use to your advantage to disallow any typing after two decimal spots after the decimal point. Maybe do something like this:

Sub CurrencyKeyPress(Sender, Key)
If Not IsNumeric(Chr(Key)) And Key <> 46 And Key <> 8 Then
Key = 0
Else
Dim intDecLoc
intDecLoc = InStr(1, Sender.Text, ".") - 1
If intDecLoc > 0 Then
If (Len(Sender.Text) - intDecLoc) > 2 And Key <> 8 Then Key = 0
End If
End If
End Sub

That should first check to see if the user is only typing correct characters into the field (numbers or decimal points), and if they are, it then checks to see if the person is typing more than two characters after the decimal point. This should give you what you want.
[Reply][Quote]
Mike Spragg
Posts: 1226
Top 10 forum poster: 1226 posts
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 07 Apr 07 3:57 AM
fiogf49gjkf0d
Or, might be slightly easier to use onExit and then round the amount to two decimals [x = round(x,2]
[Reply][Quote]
Jeff Weight
Posts: 219
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 08 Apr 07 1:30 AM
Oh, yeah, that works too... :D

Sometimes I enjoy the extra fun!
[Reply][Quote]
Mike Spragg
Posts: 1226
Top 10 forum poster: 1226 posts
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 08 Apr 07 4:34 AM
Yep, I can see that - good though !!
[Reply][Quote]
Steve Knowles
Posts: 657
Top 10 forum poster: 657 posts
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 12 Apr 07 4:14 PM
Jeff, I ran it by my team and we are going to use your function. Been trying to pimp it out to not accept two decimal points - any thoughts?
[Reply][Quote]
Mike Spragg
Posts: 1226
Top 10 forum poster: 1226 posts
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 12 Apr 07 4:17 PM
Be sure to capture the minus "-" too - otherwise, you won't be able to capture negative figures. (key = 45)
[Reply][Quote]
Jeff Weight
Posts: 219
 
Re: Format float textbox to two decimal placesYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 16 Apr 07 10:16 AM
Sorry about the late post - took a hiatus for the weekend...

You mean you want to prevent the user from using more than one decimal point? I modified the function for that and for Mike Spragg's recommendation:
Sub CurrencyKeyPress(Sender, Key)
If Not IsNumeric(Chr(Key)) And Key <> 46 And Key <> 8 And Key <> 45 Then
Key = 0
Else
Dim intDecLoc
intDecLoc = InStr(1, Sender.Text, ".") - 1
If Len(Sender.Text) > 1 And Key = 45 Then Key = 0
If intDecLoc > 0 Then If (Len(Sender.Text) - intDecLoc) > 2 And Key <> 8 Then Key = 0
If UBound(Split(Sender.Text, "-")) > 0 And Key = 45 Then Key = 0
If UBound(Split(Sender.Text, ".")) > 0 And Key = 46 Then Key = 0
End If
End Sub
[Reply][Quote]
 Page 1 of 1 
  You can subscribe to receive a daily forum digest in your user profile. View the site code of conduct for posting guidelines.

   Forum RSS Feed - Subscribe to the forum RSS feed to keep on top of the latest forum activity!
 

 
 slxdeveloper.com is brought to you courtesy of Ryan Farley & Customer FX Corporation.
 This site, and all contents herein, are Copyright © 2025 Customer FX Corporation. The information and opinions expressed here are not endorsed by Sage Software.

code of conduct | Subscribe to the slxdeveloper.com Latest Article RSS feed
   
 
page cache (param): 6/7/2025 6:43:17 AM