Results 1 to 9 of 9

Thread: Text Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Cool

    Ok here we go......if i have a text box (Text1.text) and its max length is 8 characters.....if someone types 9....how would i get a msgbox to come up on the 9th character?
    -RaY
    VB .Net 2010 (Ultimate)

  2. #2
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715

    Cool

    if the maxlenght is 8 then there is no way that someone could type 9
    NXSupport - Your one-stop source for computer help

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Even........

    Even if the max length is 8....there is still a keypress even is going on.......anyone gonna help.
    -RaY
    VB .Net 2010 (Ultimate)

  4. #4
    Guest
    Try this.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
        'If the key is not Backspace then...
        If Not KeyAscii = 8 Then
            'Display a MessageBox on the 9th character
            If Len(Text1.Text) = 8 Then MsgBox ("No more letters")
        End If
        
    End Sub

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    VB will intercept that 9th keystroke and beep. I don't think you can programatically intercept it without using the API (I'm about 90% sure, Regis!).

    If you want a MsgBox to come up on the 9th keystroke, instead of using MaxLength, you could intercept it as it happens in the KeyPress event or after it happens in the Change event. For example:

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        ' weed out control characters
        If KeyAscii < 32 Then Exit Sub
        
        If Len(Text1.Text) = 8 Then
            MsgBox "Sorry. Limit is 8."
            KeyAscii = 0
        End If
    End Sub
    - or -
    Code:
    Private Sub Text1_Change
    
        If Len(Text1.Text) > 8 Then
            MsgBox "Sorry. Limit is 8."
            Text1.Text = Left$(Text1.Text, 8)
        End If
    
    End Sub
    "It's cold gin time again ..."

    Check out my website here.

  6. #6
    Member
    Join Date
    Mar 2000
    Location
    Staffodshire, England
    Posts
    32

    Red face Works using vb5

    This works for me

    put this in general declarations

    Dim max As Integer

    Then this anywhere

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Let max = max + 1
    If max > 8 Then
    MsgBox "To many letters", vbCritical,"letter Count"
    End If
    End Sub

    Good Luck

    Mark_Dep

  7. #7
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201

    Re: Works using vb5

    Originally posted by mark_dep
    This works for me

    put this in general declarations

    Dim max As Integer

    Then this anywhere

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Let max = max + 1
    If max > 8 Then
    MsgBox "To many letters", vbCritical,"letter Count"
    End If
    End Sub

    Good Luck

    Mark_Dep
    but what if they backspace or type over some of the characters? You'd want the backspace to at least decrease the max variable, and you'd have to consider that the text box could possibly contain characters to begin with.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Thanks for the help

    I would try your suggesions when I get off work, I appreciate the replies!
    -RaY
    VB .Net 2010 (Ultimate)

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Thumbs up Works

    Megatron it works like a charm! Thanks guys!
    -RaY
    VB .Net 2010 (Ultimate)

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