Results 1 to 16 of 16

Thread: [RESOLVED] String Manipulation Help

  1. #1

    Thread Starter
    Addicted Member dbasenoob's Avatar
    Join Date
    Jan 2009
    Location
    San Pedro Laguna - Philippines
    Posts
    206

    Resolved [RESOLVED] String Manipulation Help

    how could i make pure text only when i type on a textbox? numbers must not be accept... but when i type something like this asdf123 its accept... how could i make it pure text only?

    Code:
    if isnumeric(text1.text) = true then
    msgbox "No Numbers Allowed"
    end if

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: String Manipulation Help

    You can trap keys in the textbox KeyPress event and simply ignore all numeric keys in the range of Asc("0") to Asc("9").
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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

    Re: String Manipulation Help

    Something like this: ...
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      If KeyAscii < 48 Or KeyAscii > 57 Then '~~~> If it is not a number (0 - 9) Ascii value = 48 - 57
        If KeyAscii <> 8 Then               '~~~> Allow Backspace
          KeyAscii = 0                      '~~~> Make it an invalid entry
        End If
      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,...

  4. #4

    Thread Starter
    Addicted Member dbasenoob's Avatar
    Join Date
    Jan 2009
    Location
    San Pedro Laguna - Philippines
    Posts
    206

    Re: String Manipulation Help

    Code:
    Private Sub Command1_Click()
    If IsNumeric(Text1) = True Then
        MsgBox "No Numeric Allowed", vbCritical, "warning"
        MsgBox "Must Be Text only",vbCritical, "warning"
        Text1.Text = ""
        Text1.SetFocus
        Exit Sub
        End If
    End Sub
    it allows this asdf123 when i inputted what's wrong with my code?
    also allows this asdf123asdf i want to happen is only text only must be entered

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: String Manipulation Help

    You put it in the wrong event. As akhileshbc showed, it goes in the KeyPress event of the textbox.

    I can't tell if you want to disallow numbers or only allow letters. If the latter:
    vb Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     Select Case KeyAscii
    3.         Case 65 To 90, 97 To 122 ' A-Z, a-z
    4.         Case Else: KeyAscii = 0
    5.     End Select
    6. End Sub

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: String Manipulation Help

    My Version

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        '~~> Ensures numbers are not keyed it
        '~~> Also ensure the user is not able to paste number
        If KeyAscii > 47 And KeyAscii < 58 Or _
        KeyAscii = 22 Then KeyAscii = 0
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: String Manipulation Help

    That doesn't prevent pasting from the clipboard, contrary to what the comments say. You have to handle pasting from the Change event.
    Last edited by Ellis Dee; Feb 26th, 2010 at 09:31 AM.

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

    Re: String Manipulation Help

    Using LIKE operator...
    Code:
    Private Sub Command1_Click()
      If Not Text1.Text Like "*[!0-9]*" Then
        MsgBox "Valid.. Contains only digits (0 to 9)"
      Else
        MsgBox "Contains something else..."
      End If
    End Sub
    More info: http://www.devx.com/vb2themax/Tip/18578 ...

    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,...

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: String Manipulation Help

    Here is the code for disabling paste using right mouse click... I am sure the user can take care of insert like the way I took care of Ctrl + V

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        '~~> Ensures numbers are not keyed it
        '~~> Also ensure the user is not able to paste number
        If KeyAscii > 47 And KeyAscii < 58 Or _
        KeyAscii = 22 Then KeyAscii = 0
    End Sub
    
    '~~> Disable paste using right click
    Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbRightButton Then
        Clipboard.Clear
        End If
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Addicted Member dbasenoob's Avatar
    Join Date
    Jan 2009
    Location
    San Pedro Laguna - Philippines
    Posts
    206

    Re: String Manipulation Help

    I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?

    I tried all the codes and it works

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

    Re: String Manipulation Help

    Quote Originally Posted by dbasenoob View Post
    I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?
    Could you make it clear..??? It is confusing us....

    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,...

  12. #12
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: String Manipulation Help

    Quote Originally Posted by koolsid View Post
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        '~~> Ensures numbers are not keyed it
        '~~> Also ensure the user is not able to paste number
        If KeyAscii > 47 And KeyAscii < 58 Or _
        KeyAscii = 22 Then KeyAscii = 0
    End Sub
    Pasting can also be done via Shift+Ins, which does not fire a KeyPress event.
    '~~> Disable paste using right click
    Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
    Clipboard.Clear
    End If
    End Sub
    [/CODE]
    It is a bad practice to unexpectedly clear the clipboard. For example, most of us rightfully think it's cheesy and lame that opening VB6 clears the clipboard.

    Also, the context menu (and by extension Paste) can be opened using the Context Menu key on the keyboard, which doesn't fire a KeyPress event.

    Shift+Ins and the Context Menu keys do fire KeyDown() events, though. But even still, unexpectedly clearing the clipboard is a bad practice.

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: String Manipulation Help

    Quote Originally Posted by dbasenoob View Post
    I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?

    I tried all the codes and it works
    Cannot get better than this....

    Eliis: didn't see your post... gimme a moment....

    I have updated the attachment... Now try it...
    Attached Files Attached Files
    Last edited by Siddharth Rout; Feb 26th, 2010 at 09:55 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: String Manipulation Help

    Sweet! That's full of win.

    (You should pull out the right+click code since it doesn't do anything except unexpectedly clear the clipboard.)

  15. #15
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: String Manipulation Help

    Although you should technically be allowed to paste valid text. I still maintain that clearing out invalid characters in the Change() event is the better way to handle pasting.

    Does anyone know how Microsoft handles it? Are there any limited-character textboxes in Windows or Office we can play with to see how they handle pasting invalid characters?

    EDIT: Microsoft allows invalid characters. For example, you can type letters into any numeric textbox under Tools=>Options in both Excel and Word, and if you right-click the desktop and go to the Screen Saver tab, you can type letters in the "Wait ... minutes" textbox.
    Last edited by Ellis Dee; Feb 26th, 2010 at 10:06 AM.

  16. #16
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: String Manipulation Help

    Quote Originally Posted by Ellis Dee View Post
    Sweet! That's full of win.

    (You should pull out the right+click code since it doesn't do anything except unexpectedly clear the clipboard.)
    Already updated the attachment above
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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