-
May 7th, 2011, 09:18 PM
#1
Thread Starter
Junior Member
Word and Character counter in VB6?
I'm currently working on rich text editor, which I'm planning to add loads of functions into it. I'm just not sure on how to make a Word and Character counter for it. Not sure why I want it; but it just looks cool.
Any help would be appreciated, and thanks in advance.
-
May 7th, 2011, 10:04 PM
#2
Banned
Re: Word and Character counter in VB6?
Umm im gussing ur making like a program that counts words and spaces like teachers for typing classes have ur use thats actualy a simple program to make i already made one for my sisster inlaw for her when she was learning to type for work. Is this what ur looking to do?
-
May 7th, 2011, 10:15 PM
#3
Re: Word and Character counter in VB6?
I made a quick example:
vb Code:
Option Explicit
Private Sub Command1_Click()
Dim tempArray() As String
Dim lngWordCount As Long
Dim lngCharCount As Long
Dim lngCharCountS As Long
tempArray = Split(Trim$(RichTextBox1.Text), " ")
lngWordCount = UBound(tempArray) + 1 '~~~ Number of words. ie, delimited by a single space
lngCharCount = Len(RichTextBox1.Text) '~~~ Number of characters including spaces
lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
'~~~ Display it
MsgBox "Word Count = " & lngWordCount & vbNewLine & _
"Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
"Character count(without white spaces) = " & lngCharCountS
End Sub
You could additional things or alter it suit your needs.
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,...
-
May 8th, 2011, 09:01 AM
#4
Re: Word and Character counter in VB6?
This is what I use on my improved NotePad app.
Code:
Private Sub mnuCharCount_Click()
Dim Chars As Long
Dim I As Long
Chars = 0
For I = 0 To Len(rtbText.Text) - 1
rtbText.SelStart = I
rtbText.SelLength = 1
If Asc(rtbText.SelText) > 32 And Asc(rtbText.SelText) <= 126 Then
Chars = Chars + 1
End If
Next
MsgBox "The number of CHARACTERS is:" & vbCrLf & Chars, vbInformation, "Character Count"
End Sub
And a Word count.
Code:
Private Sub mnuWordCount_Click()
Dim AsciiThisChar As Integer
Dim AsciiLastChar As Integer
Dim Words As Long
Dim I As Long
Dim LastChar As String * 1
Dim ThisChar As String * 1
rtbText.SelStart = 0
rtbText.SelLength = 1
LastChar = rtbText.SelText
Words = 0
For I = 1 To Len(rtbText.Text)
rtbText.SelStart = I
rtbText.SelLength = 1
ThisChar = rtbText.SelText
AsciiThisChar = Asc(ThisChar)
AsciiLastChar = Asc(LastChar)
If ((AsciiThisChar = 13 Or AsciiThisChar = 10) Or (AsciiThisChar >= 32 And AsciiThisChar <= 47) Or (AsciiThisChar >= 58 And AsciiThisChar <= 64) Or (AsciiThisChar >= 91 And AsciiThisChar <= 96) Or (AsciiThisChar >= 123 And AsciiThisChar <= 126)) And ((AsciiLastChar >= 48 And AsciiLastChar <= 57) Or (AsciiLastChar >= 65 And AsciiLastChar <= 90) Or (AsciiLastChar >= 97 And AsciiLastChar <= 122)) Then
Words = Words + 1
End If
LastChar = ThisChar
Next
MsgBox "The number of WORDS is: " & vbCrLf & Words, vbInformation, "Number of Words"
End Sub
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
May 8th, 2011, 09:05 AM
#5
Re: Word and Character counter in VB6?
Yes yours is pretty quick akhileshbc.
I did mine 13 years ago but I've never bothered with it since.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
May 8th, 2011, 10:26 AM
#6
Re: Word and Character counter in VB6?
Originally Posted by akhileshbc
I made a quick example:
vb Code:
Option Explicit Private Sub Command1_Click() Dim tempArray() As String Dim lngWordCount As Long Dim lngCharCount As Long Dim lngCharCountS As Long tempArray = Split(Trim$(RichTextBox1.Text), " ") lngWordCount = UBound(tempArray) + 1 '~~~ Number of words. ie, delimited by a single space lngCharCount = Len(RichTextBox1.Text) '~~~ Number of characters including spaces lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces) '~~~ Display it MsgBox "Word Count = " & lngWordCount & vbNewLine & _ "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _ "Character count(without white spaces) = " & lngCharCountS End Sub
You could additional things or alter it suit your needs.
I would modify that slightly in case there are extra spaces like the two spaces after periods that some people use.
VB Code:
Private Sub Command1_Click() Dim tempArray() As String Dim lngWordCount As Long Dim lngCharCount As Long Dim lngCharCountS As Long Dim strTemp1 As String Dim strTemp2 As String strTemp1 = RichTextBox1.Text Do Until LenB(strTemp1) = LenB(strTemp2) strTemp2 = Replace(strTemp1, " ", " ") ' replace 2 spaces with 1 space strTemp1 = strTemp2 Loop tempArray = Split(Trim$(strTemp1), " ") lngWordCount = UBound(tempArray) + 1 '~~~ Number of words. ie, delimited by a single space lngCharCount = Len(RichTextBox1.Text) '~~~ Number of characters including spaces lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces) '~~~ Display it MsgBox "Word Count = " & lngWordCount & vbNewLine & _ "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _ "Character count(without white spaces) = " & lngCharCountS End Sub
-
May 8th, 2011, 10:31 AM
#7
Addicted Member
Re: Word and Character counter in VB6?
Here's a very fast method using the CopyMemory API:
In the Declarations section put:
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)
In a Module add this function:
Code:
Public Function WordCount(Text As String) As Long
Dim dest() As Byte
Dim i As Long
If LenB(Text) Then
' Move the string's byte array into dest ()
ReDim dest(LenB(Text))
CopyMemory dest(0), ByVal StrPtr(Text), LenB(Text) - 1
' Now loop through the array and count the words
For i = 0 To UBound(dest) Step 2
If dest(i) > 32 Then
Do Until dest(i) < 33
i = i + 2
Loop
WordCount = WordCount + 1
End If
Next i
Erase dest
Else
WordCount = 0
End If
End Function
Since Rich Text Editors have been done many times, you may benefit from downloading and viewing code on existing projects. The following editor project will not only generate Word Count but character count (different than word count) and line count... plus many other features good editors contain:
http://www.planet-source-code.com/vb...69067&lngWId=1
Good luck,
Tom
-
May 8th, 2011, 12:59 PM
#8
Re: Word and Character counter in VB6?
@Keithuk, @MartinLiss and @Tom Moran : Nice code
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,...
-
May 8th, 2011, 01:53 PM
#9
Re: Word and Character counter in VB6?
Originally Posted by akhileshbc
@Keithuk, @MartinLiss and @Tom Moran : Nice code
Thanks, but my code was wrong. It only caught the first multiple space.
VB Code:
Private Sub Command1_Click()
Dim tempArray() As String
Dim lngWordCount As Long
Dim lngCharCount As Long
Dim lngCharCountS As Long
Dim strTemp As String
Dim intLen As Integer
strTemp = RichTextBox1.Text
Do Until Len(strTemp) = intLen
intLen = Len(strTemp)
strTemp = Replace(strTemp, " ", " ")
Loop
tempArray = Split(Trim$(strTemp), " ")
lngWordCount = UBound(tempArray) + 1 '~~~ Number of words. ie, delimited by a single space
lngCharCount = Len(RichTextBox1.Text) '~~~ Number of characters including spaces
lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
'~~~ Display it
MsgBox "Word Count = " & lngWordCount & vbNewLine & _
"Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
"Character count(without white spaces) = " & lngCharCountS
End Sub
-
May 9th, 2011, 09:59 AM
#10
Thread Starter
Junior Member
Re: Word and Character counter in VB6?
@akhileshbc: thanks for your help. By the way, I put your forum name into one of my vb comments.
-
Oct 23rd, 2012, 08:15 AM
#11
Junior Member
Re: Word and Character counter in VB6?
hey there
I'm new here and this code trapped me, so I tried to make it show the number of word and number of letters for each word in the text, but I failed xD
can any one help.
-
Oct 23rd, 2012, 08:22 AM
#12
Re: Word and Character counter in VB6?
First of all you should start a new thread for your question. This one is over a year old.
Second you need to tell us what you are trying to do and what problem(s) you are having.
Third you need to show us some of the code you used that is giving you the problem.
We can't help if we don't have any info on your problem.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|