VB4 newbie help: Reading a real into a textbox 
Author Message
 VB4 newbie help: Reading a real into a textbox

OK, so I am new to the VB caper, but I am trying.

I want to do something simple like restrict a users input into
a textbox, making sure that it is a real (or integer). I
know that you can use the keypress event to limit the allowable
keystrokes to numbers and the decimal, but I can't stop the
user from entering 3.14.159.

I know I am missing something. Can someone please help me. By
the way, I'm using VB4 standard edition.
--
---------------------------------------------------------------------
Brian Hoover http://www.*-*-*.com/ ~hoover   |  Multiline BBS

---------------------------------------------------------------------



Mon, 25 Oct 1999 03:00:00 GMT  
 VB4 newbie help: Reading a real into a textbox

Quote:

>I want to do something simple like restrict a users input into
>a textbox, making sure that it is a real (or integer). I
>know that you can use the keypress event to limit the allowable
>keystrokes to numbers and the decimal, but I can't stop the
>user from entering 3.14.159.

You could test if the text already contains a "." in the keyPress event:

        Sub Text1_KeyPress(keyAscii as integer)
                If keyAscii= Asc(".") then
                        if instr(text1,".") then
                                beep
                                keyAscii=0
                        endif
                endif
        end sub

You should also limit entering "-" to at the front of the text, using
Text1.SelStart.

Be careful about using CSng(Text) + error catching, since this won't
allow you to enter a sibngle "-".

Gent (Ghent, Gand),
Belgium,
Europe,
3rd planet from the sun.



Tue, 26 Oct 1999 03:00:00 GMT  
 VB4 newbie help: Reading a real into a textbox

The Masked Edit-control (included in VB 4.0) can be used here. Check it
out in help.



Tue, 26 Oct 1999 03:00:00 GMT  
 VB4 newbie help: Reading a real into a textbox

Use the following code:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        If InStr(Text1.Text, ".") = 0 And KeyAscii = 46 Then
            Exit Sub
        End If
        KeyAscii = 0
    End If
End Sub

This will allow for only number 1 decimal point anywhere in the string
and the use of the backspace key.

        -Steve
Innovative Communications

On Thu, 08 May 1997 16:33:35 +0800, Brian Hoover

Quote:

>OK, so I am new to the VB caper, but I am trying.

>I want to do something simple like restrict a users input into
>a textbox, making sure that it is a real (or integer). I
>know that you can use the keypress event to limit the allowable
>keystrokes to numbers and the decimal, but I can't stop the
>user from entering 3.14.159.

>I know I am missing something. Can someone please help me. By
>the way, I'm using VB4 standard edition.
>--
>---------------------------------------------------------------------
>Brian Hoover http://www.multiline.com.au/~hoover   |  Multiline BBS

>---------------------------------------------------------------------



Tue, 26 Oct 1999 03:00:00 GMT  
 VB4 newbie help: Reading a real into a textbox

Use the following code:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        If InStr(Text1.Text, ".") = 0 And KeyAscii = 46 Then
            Exit Sub
        End If
        KeyAscii = 0
    End If
End Sub

This will allow for only number 1 decimal point anywhere in the string
and the use of the backspace key.

        -Steve
Innovative Communications

On Thu, 08 May 1997 16:33:35 +0800, Brian Hoover

Quote:

>OK, so I am new to the VB caper, but I am trying.

>I want to do something simple like restrict a users input into
>a textbox, making sure that it is a real (or integer). I
>know that you can use the keypress event to limit the allowable
>keystrokes to numbers and the decimal, but I can't stop the
>user from entering 3.14.159.

>I know I am missing something. Can someone please help me. By
>the way, I'm using VB4 standard edition.
>--
>---------------------------------------------------------------------
>Brian Hoover http://www.multiline.com.au/~hoover   |  Multiline BBS

>---------------------------------------------------------------------



Tue, 26 Oct 1999 03:00:00 GMT  
 VB4 newbie help: Reading a real into a textbox

Use the following code:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        If InStr(Text1.Text, ".") = 0 And KeyAscii = 46 Then
            Exit Sub
        End If
        KeyAscii = 0
    End If
End Sub

This will allow for only number 1 decimal point anywhere in the string
and the use of the backspace key.

        -Steve
Innovative Communications

On Thu, 08 May 1997 16:33:35 +0800, Brian Hoover

Quote:

>OK, so I am new to the VB caper, but I am trying.

>I want to do something simple like restrict a users input into
>a textbox, making sure that it is a real (or integer). I
>know that you can use the keypress event to limit the allowable
>keystrokes to numbers and the decimal, but I can't stop the
>user from entering 3.14.159.

>I know I am missing something. Can someone please help me. By
>the way, I'm using VB4 standard edition.
>--
>---------------------------------------------------------------------
>Brian Hoover http://www.multiline.com.au/~hoover   |  Multiline BBS

>---------------------------------------------------------------------



Tue, 26 Oct 1999 03:00:00 GMT  
 VB4 newbie help: Reading a real into a textbox


Quote:

>Use the following code:

>Private Sub Text1_KeyPress(KeyAscii As Integer)
>    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
>        If InStr(Text1.Text, ".") = 0 And KeyAscii = 46 Then
>            Exit Sub
>        End If
>        KeyAscii = 0
>    End If
>End Sub

>This will allow for only number 1 decimal point anywhere in the string
>and the use of the backspace key.

Of course, this will require a bit of modification to allow the
optional '-' character only at the beginning of the string, but
your solution is quite clever!
--
 From the catapult of J.D. Baldwin  |+| "If anyone disagrees with anything I

 _|70|___:::)=}-  for PGP public    |+| retract it, but also to deny under
 \      /         key information.  |+| oath that I ever said it." --T. Lehrer
***~~~~-----------------------------------------------------------------------


Sun, 31 Oct 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Help please with read/write from textbox control - VB4

2. Newbie Needs Help on Textbox

3. Newbie needs help with TextBox

4. Newbie needs help with Loop to textBox

5. Newbie Help ...Adding TextBoxes At RunTime

6. How is CR handled by real textbox???

7. Multiple user textbox updates (real-time)

8. How is CR handled by real textbox???

9. Read-Only TextBox help

10. HELP!:Read-only textbox & Autoexec File Icons

11. A real newbie question

12. real newbie

 

 
Powered by phpBB® Forum Software