Results 1 to 5 of 5

Thread: [RESOLVED] Text Box Keypress Event problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    17

    Resolved [RESOLVED] Text Box Keypress Event problem

    Hi! I'm trying to make my program count the number of words in my text box by counting the number of spaces, I tried this:

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If KeyAscii = 32 Then
    3. spacecount = spacecount + 1
    4. End If
    5. Label1.Caption = "Word Count:" & spacecount
    6. End Sub

    but whenever I type a non space character, it seems that spacecount is reset to 0. Oh yeah, I know the algorithm of my program for counting words is very problematic, I'll fix that later.

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Text Box Keypress Event problem

    I think it would be better to loop through the text and get the words instead of trying to count the spaces as if they press space twice or at the beginning it will give a false count
    Chris

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Text Box Keypress Event problem

    You need to declare the spacecount variable. Either in the declarations at the top of the form, or make it static in the procedure:

    VB Code:
    1. Private iSpaceCount As Integer
    2.  
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4.   If KeyAscii = 32 Then iSpaceCount = iSpaceCount + 1
    5.   Label1.Caption = "Word Count:" & iSpaceCount
    6. End Sub
    Or:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.   Static iSpaceCount As Integer
    3.   If KeyAscii = 32 Then iSpaceCount = iSpaceCount + 1
    4.   Label1.Caption = "Word Count:" & iSpaceCount
    5. End Sub

    But, yes there are several other reasons why that code won't produce the right results.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Text Box Keypress Event problem

    here's a little something I just knocked up:
    VB Code:
    1. Private Sub Text1_Change()
    2.     Dim N As Long, lCount As Long
    3.     Dim bSpace As Boolean, bBoolean As Boolean
    4.     Dim bytArr() As Byte
    5.    
    6.     bytArr = Text1.Text
    7.     For N = LBound(bytArr) To UBound(bytArr) Step 2
    8.         bSpace = (bytArr(N) = 32) Or (bytArr(N) = 10) Or (bytArr(N) = 13)
    9.         If N > 4 Then
    10.             If ((bytArr(N) = 10) Or (bytArr(N) = 13)) And (bytArr(N - 2) = 45 Or bytArr(N - 4) = 45) Then bSpace = False
    11.         End If
    12.         If bBoolean Then lCount = lCount - (bBoolean = bSpace)
    13.         bBoolean = Not bSpace
    14.     Next N
    15.     Me.Caption = "Word Count: " & lCount - bBoolean
    16. End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    17

    Re: Text Box Keypress Event problem

    Ah yes I remember about making the variable static. Thnx everyone!

    *marks as resolved*

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