Results 1 to 8 of 8

Thread: [RESOLVED] disallowing text input in a textbox

  1. #1

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    Resolved [RESOLVED] disallowing text input in a textbox

    ok so the first one was a success, but now i want to disallow the user from typing anything but numbers into the textbox, since you cannot multiply, add, divide or subtract a letter, or any other symbol. if there is no way to actually stop the user from typing these, how can i make an elseif statement to only allow the calculator to allow a number between 1 and 13034431, and nothing else, and then display

    Code:
     msgbox "Please type a proper number"
    Last edited by clownboy; Aug 22nd, 2008 at 05:57 AM.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: disallowing text input in a textbox

    Something like this...????
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii >= 48 And KeyAscii <= 57 Then
    '
    Else
    KeyAscii = 0
    End If
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: disallowing text input in a textbox

    There are a few ways of doing this.

    Easiest would be to add this in your _LostFocus() Event

    Code:
    If IsNumeric(text1.text) = False then
    msgbox "Please type a proper number"
    text1.setfocus
    End If
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: disallowing text input in a textbox

    Or something like this..????
    Code:
    Private Sub Text1_Change()
    If IsNumeric(Text1.Text) = False Then
        MsgBox "Only numbers are allowed..!!"
        Text1.SetFocus
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
        Exit Sub
    End If
    
    If Val(Text1.Text) >= 1 And Val(Text1.Text) <= 13034431 Then
    '
    Else
    Text1.Text = "1"
    Text1.SetFocus
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
    MsgBox "The limit is from 1 to 13034431", vbCritical, "Exceeds limit...!"
    End If
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    Re: disallowing text input in a textbox

    akhileshbc, both were good, but disallowed the use of backspace for the first set of code, and the message came up when backspace was pressed for the second one.

    some1uk03, worked, but the error still came up after the message arrived.

    any solution to allow the backspace as well?
    sorry am n00b

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: disallowing text input in a textbox

    Try this...
    Code:
    Private Sub Text1_Change()
    If Trim(Text1.Text) = "" Then Exit Sub     '''''''''''''''''''''''''''''''''''''''''newly introduced
    If IsNumeric(Text1.Text) = False Then
        MsgBox "Only numbers are allowed..!!"
        Text1.SetFocus
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
        Exit Sub
    End If
    
    If Val(Text1.Text) >= 1 And Val(Text1.Text) <= 13034431 Then
    '
    Else
    Text1.Text = "1"
    Text1.SetFocus
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
    MsgBox "The limit is from 1 to 13034431", vbCritical, "Exceeds limit...!"
    End If
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    Re: disallowing text input in a textbox

    is there anything you can't do??
    legend mate, i'll keep you yet. ^^

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] disallowing text input in a textbox

    Try this (backspace is 8)
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 _
        And KeyAscii <> 8 Then
        MsgBox "Only numbers are allowed for this entries"
        KeyAscii = 0
        Exit Sub
    End If
    End Sub
    
    'also, in the change event put this code
    'to prevent pasting in non numeric text
    Private Sub Text1_Change()
    If Not IsNumeric(Text1.Text) Then
       MsgBox "Only Numbers Are Allowed"
       Text1.Text = vbNullString
       Exit Sub
    End If

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