Page 1 of 1

Word Counter

Posted: Sun Jan 09, 2011 4:16 pm
by code it
Design:
word counter.JPG
Code:
word counter code.JPG
USE THIS CODE:(THANKS MANDAI FOR THE NOTICE!
Code: Select all
       Dim spaceArray As String() = RichTextBox1.Text.Replace(vbLf, " ").Split(" "c)
        Dim wordcount As Integer = 0
        For i As Integer = 0 To spaceArray.Length - 1
            If spaceArray(i).Length > 0 Then wordcount += 1
        Next
      Label1.Text = "Words: " & wordcount
DONT USE THE ONE AT THE BOTTOM!!
Code: Select all
Public Class Form1
    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim wordcount As Integer
        Dim num As String() = RichTextBox1.Text.Split(" ")
        wordcount = num.Length
        Label1.Text = "Words:  " & wordcount
        If RichTextBox1.Text = "" Then
            Label1.Text = "Words: 0"
        Else
            Label1.Text = "Words:  " & wordcount
        End If
    End Sub
End Class

Screenshots:
word counter2.JPG

Please +Rep me!

Re: Word Counter

Posted: Sun Jan 09, 2011 6:24 pm
by mandai
This counts space characters as words, plus it doesn't continue on newlines.
I made some code that will only count the words: viewtopic.php?f=32&t=3776&p=26595#p26595

Re: Word Counter

Posted: Sun Jan 09, 2011 6:28 pm
by Axel
This checks for spaces,
if you have a string "hello world" wich contain 2 spaces, the word count will be 3 i guess

Re: Word Counter

Posted: Sun Jan 09, 2011 6:29 pm
by mandai
But a word isn't valid unless its length is greater than 0.

Re: Word Counter

Posted: Sun Jan 09, 2011 6:58 pm
by code it
Nevermind!

Thanks mandai for foxing my mistake