Results 1 to 13 of 13

Thread: how to enable only number to a textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156

    Post

    I know that it related some how to keypress event ,
    but I tryed and couldn't worked it out.
    The MORE I get to know,
    I realize that I know NOTHING !

  2. #2
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    click on the text box, then goto the properties window, and click on Data Format, and chose numbers
    NXSupport - Your one-stop source for computer help

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156

    Thumbs down

    well , it doesn't work !!
    again I need that the user want be able to insert numbers
    to the textbox.
    The MORE I get to know,
    I realize that I know NOTHING !

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    ok, this isn't the best but here it is (no gaurantee):





    [code]
    'put this into the declerations:
    dim inttxt as interger
    ' end declerations


    'put this into the text change function:
    if text1.text = "0", or "1", or "2", or "3", or "4", or "5", or "6", or "7", or "8", or "9" then
    text1.visible = true ' just a command to waste time

    inttxt = text1.text

    else:
    text1.text = ""

    'this might not work
    NXSupport - Your one-stop source for computer help

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    There are a lot of ways to do this, but you should find that this is pretty bullet proof. What you do is just check the Asc entered into the box on keypress. In this example, Text1.text will only accept 1-9, a backspace (character 8) and a space (character 32).

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    CheckChar = KeyAscii
    If CheckChar = 8 Then
    Exit Sub
    ElseIf CheckChar = 32 Then
    Exit Sub
    ElseIf CheckChar < 48 Or CheckChar > 57 Then
        MsgBox "You can only enter a numeric in this box", vbApplicationModal, "Numeric violation"
        KeyAscii = 0
        Text1.Text = Text1.Text & Chr(KeyAscii)
    End If
    
    End Sub

    Hope that this helps

    (Using VB 6 SP 3)

  6. #6
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321
    Well, you can use the Isnumeric funtion too.

    This should be secure.

    Code:
    Private Sub Text1_Change()
    If IsNumeric(Text1.Text) Then
        'Code when it is
        '(Nothing)
    Else
        'Code when it's not
        MsgBox "Only numbers! Try again!"
        Text1.Text = ""
        Text1.SetFocus
    End If
    End Sub


    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  7. #7
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321
    Also, just pointing out.

    The method which reeset refers too wouldn't allow the user to paste a number from the clipboard.
    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  8. #8
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    Good point. But you could easily fix that by just adding exeptions to new values.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    CheckChar = KeyAscii
    If CheckChar = 8 Then
    Exit Sub
    ElseIf CheckChar = 32 Then
    Exit Sub
    ElseIf CheckChar = 22 Then  
    'If user tries to paste
    
    svalue = Clipboard.GetText
        For x = 1 To Len(svalue)
            sNextChar = Asc(Mid(svalue, x, 1))
            
            If sNextChar < 48 Or sNextChar > 57 Then
                MsgBox "You can only enter a numeric in this box", vbApplicationModal, "Numeric violation"
                Clipboard.Clear
                Exit Sub
            End If
        Next
    ElseIf CheckChar = 3 Then
    'if user try's to copy
    Exit Sub
    ElseIf CheckChar < 48 Or CheckChar > 57 Then
        MsgBox "You can only enter a numeric in this box", vbApplicationModal, "Numeric violation"
        KeyAscii = 0
        Text1.Text = Text1.Text & Chr(KeyAscii)
    End If
    
    End Sub
    Thanks for pointing that out.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    This code will work with negative values and commas
    Code:
    Private Sub Text1_Change(): Dim temp&
        temp = Text1.SelStart
        If IsNumeric(Text1) Then Text1.Tag = Text1 Else Text1 = Text1.Tag
        Text1.SelStart = temp
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile API function

    I know this can be done just with a simple API function, but I can remeber the Function name, if anyone found it just email.

    Thanks

  11. #11
    Lively Member Xero's Avatar
    Join Date
    Feb 2000
    Posts
    75
    VbSquare posted a tip about doing this same thing. Here's the code:
    Code:
    'Declarations
    Public Declare Function GetWindowLong& Lib "user32" Alias _
    "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
    
    Public Declare Function SetWindowLong& Lib "user32" Alias _
    "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
    ByVal dwNewLong As Long)
    
    Public Const ES_NUMBER = &H2000&
    Public Const GWL_STYLE = (-16)
    
    'Textbox Change
    Private Sub Text1_Change()
        Dim tmpValue&
        Dim fAlignment&
        Dim ret&
        fAlignment& = ES_NUMBER
        tmpValue& = GetWindowLong&(txtRefreshRate.hwnd, GWL_STYLE)
        ret& = SetWindowLong&(txtRefreshRate.hwnd, GWL_STYLE, tmpValue& Or fAlignment&)
        txtRefreshRate.Refresh
    End Sub
    Well, there ya go! Works no problem.

  12. #12
    Addicted Member
    Join Date
    Mar 2000
    Location
    Gainesville, FL
    Posts
    131
    Wow, such overkill!

    try this:

    Code:
    text1.text = val(text1.text)
    hehe. Easy.


  13. #13
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    Thanks Xero! that the power of API.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width