#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Smile Help with my word count code


    Private Sub btnCount_Click(sender As System.Object, e As System.EventArgs) Handles btnCount.Click
    ' Declare variables
    Dim str As String
    Dim i As Integer
    Dim l As Integer
    Dim Words As Integer

    str = txtWordBox.Text
    str = LTrim(str) ' Removes blank spaces at the beginning of text
    str = RTrim(str) ' Removes blank spaces at the end of text
    l = str.Length
    i = 0
    Words = 0

    While (i < l)
    If str(i) = " " Then
    Words = Words + 1
    i = i + 1
    While str(i) = " " ' Remove more than 1 blank space
    i = i + 1
    End While
    Else
    i = i + 1
    End If
    End While

    Words = Words + 1 ' Add the last word

    txtCount.Text = ("" & Words) ' Store the count in the textbox

    End Sub

    This does everything I need it to except count the words after tab or enter buttons on keyboard are hit. Does anyone know how to account for that?
  2. #2
  3. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    Interesting approach. I would simply use Split() to split the words into an array and use UBound() on the array (+1) to give you the word count.

    ie


    Code:
    Sub ExampleCount()
        Dim strSentence As String
        Dim strWords() As String
        Dim lngWordCount As Long
        strSentence = "The quick brown fox jumped over the lazy dog."
        strWords = Split(strSentence, " ")
        lngWordCount = UBound(strWords) + 1 ' assumes zero bound array
        MsgBox "Your sentence has " & lngWordCount & " words."
    End Sub
    But it sounds like what you need for your question is to use the keypress event or key up/down event
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    Originally Posted by medialint
    Interesting approach. I would simply use Split() to split the words into an array and use UBound() on the array (+1) to give you the word count.

    But it sounds like what you need for your question is to use the keypress event or key up/down event
    I think that is what the poster is after.
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Dim strSentence As String
        Dim strWords() As String
        Dim lngWordCount As Long
        If KeyAscii = 13 Or KeyAscii = 9 Then
            strSentence = Trim(Text1.Text)
            strWords = Split(strSentence, " ")
            lngWordCount = UBound(strWords) + 1 ' assumes zero bound array
            MsgBox "Your sentence has " & lngWordCount & " words."
        End If
    End Sub
  6. #4
  7. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,439
    Rep Power
    4539
    Another alternative may be to use a vb regex object. regex can identify words based on multiple delimiter characters.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester

IMN logo majestic logo threadwatch logo seochat tools logo