|
-
Aug 25th, 2006, 08:32 AM
#1
Thread Starter
New Member
limiting the decimal point to 1
I find that most code to limit the decimal point to one is written similar to this.
If KeyAscii = 46 And InStr(Text1, ".") > 0 Then
KeyAscii = 0
Exit Sub
or
Case 8, 48 To 57, 46 And InStr(TextBox, ".") = 0
The downside is that there must be a integer before the decimal point for it to function. Is there no way to limit the decimal point to one AND have the decimal point be the first digit entered? An example would be something less than 1 like .625
I have tried all that I know so far to no avail. Any ideas?
Thanks
Last edited by Davek1; Aug 25th, 2006 at 09:41 AM.
-
Aug 25th, 2006, 08:48 AM
#2
Re: limiting the decimal point to 1
Here is another approach - it's not perfect (just a quick sample) so you may need to work with code written in the Text1_Change() evnt handler:
VB Code:
Option Explicit
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER = &H2000&
Private Declare Function GetWindowLong _
Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Sub Form_Load()
'========================
Dim curStyle&, newStyle&
'allow numeric entries only
Text1.Text = ""
curStyle = GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
newStyle = SetWindowLong(Text1.hwnd, GWL_STYLE, curStyle)
End Sub
Private Sub Text1_Change()
If IsNumeric(Text1.Text) Then
Text1.Text = Format(Text1.Text, "0.0")
End If
End Sub
-
Aug 25th, 2006, 08:50 AM
#3
Re: limiting the decimal point to 1
Also, take a look at Martin's NumberTextbox sample.
-
Aug 25th, 2006, 09:00 AM
#4
Re: limiting the decimal point to 1
Using KeyPress it would be
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim lPos As Long
lPos = InStr(Text1.Text, ".")
Select Case KeyAscii
Case Asc("0") To Asc("9")
If lPos And Text1.SelStart >= lPos And Len(Text1.Text) - lPos > 0 Then
KeyAscii = 0
End If
Case Asc(".")
If lPos Then KeyAscii = 0
Case Else
If KeyAscii <> vbKeyBack Then KeyAscii = 0
End Select
End Sub
Last edited by jcis; Aug 25th, 2006 at 09:17 AM.
-
Aug 25th, 2006, 10:30 AM
#5
Thread Starter
New Member
Re: limiting the decimal point to 1
JCIS,
For an input of .625, I still have to enter an integer first. like this > 0.625
I want to avoid the first 0 as it is not a natural thing to input.
I am working with an array and have had to add "index as integer" to the first line to get it to function. Same holds true for the other code variations.
VB Code:
Private Sub text1_KeyPress(index As Integer, KeyAscii As Integer)
Maybe this "index as integer" makes it not work as intended?
Thanks
Dave
Last edited by Davek1; Aug 25th, 2006 at 10:36 AM.
-
Aug 25th, 2006, 01:23 PM
#6
Re: limiting the decimal point to 1
I see you are using a control array, but im not sure if I understand what you want, this will allow 1 decimal separator and the number must start with (.)
VB Code:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
Dim lPos As Long
lPos = InStr(Text1.Item(Index).Text, ".")
Select Case KeyAscii
Case Asc("0") To Asc("9")
If Len(Text1.Item(Index).Text) = 0 Then KeyAscii = 0
Case Asc(".")
If lPos Then KeyAscii = 0
Case Else
If KeyAscii <> vbKeyBack Then KeyAscii = 0
End Select
End Sub
-
Aug 25th, 2006, 02:30 PM
#7
Hyperactive Member
Re: limiting the decimal point to 1
Have you thought of using a masked edit box?
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
|