Results 1 to 8 of 8

Thread: Just quickly...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    87

    Just quickly...

    Is there any way (preferbly non-API) which disallows the user from entering anything other than digits into a standard text box?

    Thanks, Zeka
    "Most cars on our roads have only one occupant, usually the driver."
    - Carol Malia, BBC Anchorwoman

    "I do not like this word "bomb." It is not a bomb. It is a device that is exploding."
    - Jacques le Blanc, French ambassador on nuclear weapons

    "Solutions are not the answer."
    - Richard Nixon, former U.S. President

  2. #2
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Just quickly...

    use the keypress event of the textbox

    VB Code:
    1. Private Sub txt_Keypress(Keyascii As Integer)
    2.     if not (keyascii >= vbKey 0 and Keyascii <= vbkey9) then
    3.         keyascii = 0
    4.     end if
    5. End Sub
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  3. #3
    Addicted Member
    Join Date
    Mar 2006
    Posts
    178

    Re: Just quickly...

    Use the textbox Validate event.

    VB Code:
    1. Private Sub txtTextBox_Validate(Cancel As Boolean)
    2.     If Not IsNumeric(txtTextBox.Text) Then
    3.         Cancel = True
    4.     End If
    5. End Sub

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Just quickly...

    This code will take care of paste operations too.
    VB Code:
    1. Option Explicit
    2. 'to store the lastText that was present in
    3. 'the text box before user changed the text
    4. Dim LastText As String
    5. 'to check if we are running the Change Event code second time
    6. Dim SecondTime As Boolean
    7. 'to store the last cursor positon in the textbox
    8. Dim lastPosition As Long
    9.  
    10. Private Sub Text1_Change()
    11.     'if everything has been deleted or minus was keyed in first
    12.     'then don't do anything
    13.     If Text1.Text = "" Or Text1.Text = "-" Then
    14.         'set the lasttext
    15.         LastText = Text1.Text
    16.         Exit Sub
    17.     End If
    18.     'if the method was not called second time
    19.     If Not SecondTime Then
    20.         'check if the text box is numeric
    21.         If Not IsNumeric(Text1.Text) Then
    22.             'if not then
    23.             SecondTime = True
    24.             'set the text to the previous text present in the text box
    25.             Text1.Text = LastText
    26.             'set the cursor position back to the original
    27.             Text1.SelStart = lastPosition
    28.             Beep
    29.         Else
    30.             LastText = Text1.Text
    31.         End If
    32.     End If
    33.     SecondTime = False
    34. End Sub
    35.  
    36. Private Sub Text1_GotFocus()
    37.     'get the last text and store it
    38.     LastText = Text1.Text
    39. End Sub
    40.  
    41. Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    42.     'save the last position before
    43.     lastPosition = Text1.SelStart
    44. End Sub
    45.  
    46. Private Sub Text1_KeyPress(KeyAscii As Integer)
    47.     'don't allow spaces
    48.     If KeyAscii = vbKeySpace Then
    49.         KeyAscii = 0
    50.         Beep
    51.     End If
    52.     'save the last position before
    53.     lastPosition = Text1.SelStart
    54. End Sub
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5
    Addicted Member
    Join Date
    Mar 2006
    Posts
    178

    Re: Just quickly...

    Shuja Ali

    That seems a lot of code just to validate numbers being entered. Surely the Validate event would validate pasted text when the control loses focus? As long as the control about to receive the focus has its CausesValidation property set to True.

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Just quickly...

    Quote Originally Posted by davidbishton
    Shuja Ali

    That seems a lot of code just to validate numbers being entered. Surely the Validate event would validate pasted text when the control loses focus? As long as the control about to receive the focus has its CausesValidation property set to True.
    You are right to some extent. But the OP has asked for this
    Quote Originally Posted by Zeka
    Is there any way (preferbly non-API) which disallows the user from entering anything other than digits into a standard text box?
    Which means that he/she doesn't want theuser to enter anything other than numbers in the textbox.

    There is nothing wrond with your way of doing it, but it will allow the use to enter anything in the textbox and when the textbox looses focus it will display the message.

    The code that I posted will not allow user to enter anything other than pure umbers and even will not allow user to paste anything that is non-numeric in the textbox. This is the reason why it is so lengthy.

    Another easier way would be to use a masked edit control. You literally don't have to write any code in that case.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7
    Addicted Member
    Join Date
    Mar 2006
    Posts
    178

    Re: Just quickly...

    Ahh yes - I see what it's doing. It's more of a sophisticated way of checking.
    No problems.

  8. #8
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: Just quickly...

    or get the numberbox control in the codes section by martin liss .if that could help u lessen ur code !!
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

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