Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 16th, 2012, 08:44 AM
ajl232 ajl232 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 3 ajl232 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 38 m 43 sec
Reputation 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?

Reply With Quote
  #2  
Old May 16th, 2012, 10:43 AM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,938 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 13 m 33 sec
Reputation Power: 8490
Facebook
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

Reply With Quote
  #3  
Old May 16th, 2012, 08:48 PM
couttsj couttsj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 165 couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 16 h 27 m
Reputation Power: 43
Quote:
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

Reply With Quote
  #4  
Old May 16th, 2012, 09:37 PM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Jun 2003
Posts: 14,233 Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 14 h 15 m 56 sec
Reputation Power: 4445
Another alternative may be to use a vb regex object. regex can identify words based on multiple delimiter characters.
__________________
======
Doug G
======
It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Help with my word count code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap