6/7/2025 6:21:40 AM
|
|
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!
Forum to discuss usage & tips for SalesLogix controls and other 3rd party ActiveX controls. View the code of conduct for posting guidelines.
|
|
|
|
Format float textbox to two decimal places
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 |
|
|
|
Re: Format float textbox to two decimal places
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.
|
|
|
| |
| |
| |
| |
| |
|
Re: Format float textbox to two decimal places
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 |
|
|
|
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!
|
|
|
|
|
|
|
|