How Do I do this with a richtextbox and make it display either character or word count in another textbox
Thanks for your help,
lavarock09
Printable View
How Do I do this with a richtextbox and make it display either character or word count in another textbox
Thanks for your help,
lavarock09
To find the length of the text in a textbox you can use :
VB Code:
RichTextBox.Text = Len(Text1.Text)
That should be in the _Change event of Text1.
For word count you can do something like this (if you assume that every word is separated by a space and not other characters) :
VB Code:
Private Sub Text1_Change() Text2.Text = UBound(Split(Text1.Text, " ")) End Sub
To take manavo11's suggestion just a little step further, try this:VB Code:
Private Function GetWordCount(ByVal pstrText As String) As Long pstrText = Trim(Replace(pstrText, "-" & vbNewLine, "")) pstrText = Trim(Replace(pstrText, vbNewLine, " ")) Do While pstrText Like "* *" pstrText = Replace(pstrText, " ", " ") Loop GetWordCount = 1 + UBound(Split(pstrText, " ")) End Function Private Sub Command1_Click() MsgBox GetWordCount(RichTextBox1.Text) End Sub
Excellent, Exactly what I wanted,
Thanks