rtf box word and character counts

I am looking into learning how to do word and or character counts in rtf box.

found this

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2005_ta/html/OfficeVSTO2005WordOM.asp

on using word object model to try it.

Anyone have any references otherwise? web search hasnt come up with anything educational for me at all.

Is using word object the best or only approach?

Was going try do a search for spaces between word sort of thing.

[749 byte] By [CharlieRussell] at [2007-12-24]
# 1

RichTextBoxFinds is the thing to try for RichTextBoxes.

See note in code below to change the 'replace all' sub.

Sorry it is not more specific, but it can be used as an add

on form to a solution that uses a RichTextBox.

PublicClass FindForm

Inherits System.Windows.Forms.Form

#Region" Windows Form Designer generated code "

PublicSubNew()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

EndSub

'Form overrides dispose to clean up the component list.

ProtectedOverloadsOverridesSub Dispose(ByVal disposingAsBoolean)

If disposingThen

IfNot (componentsIsNothing)Then

components.Dispose()

EndIf

EndIf

MyBase.Dispose(disposing)

EndSub

FriendWithEvents bttnFindAs System.Windows.Forms.Button

FriendWithEvents bttnFindNextAs System.Windows.Forms.Button

FriendWithEvents bttnReplaceAs System.Windows.Forms.Button

FriendWithEvents bttnReplaceAllAs System.Windows.Forms.Button

FriendWithEvents txtSearchWordAs System.Windows.Forms.TextBox

FriendWithEvents txtReplaceWordAs System.Windows.Forms.TextBox

FriendWithEvents chkCaseAs System.Windows.Forms.CheckBox

FriendWithEvents chkWordAs System.Windows.Forms.CheckBox

'Required by the Windows Form Designer

Private componentsAs System.ComponentModel.Container

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()>PrivateSub InitializeComponent()

Me.bttnReplace =New System.Windows.Forms.Button

Me.bttnFindNext =New System.Windows.Forms.Button

Me.chkWord =New System.Windows.Forms.CheckBox

Me.bttnFind =New System.Windows.Forms.Button

Me.txtSearchWord =New System.Windows.Forms.TextBox

Me.txtReplaceWord =New System.Windows.Forms.TextBox

Me.chkCase =New System.Windows.Forms.CheckBox

Me.bttnReplaceAll =New System.Windows.Forms.Button

Me.SuspendLayout()

'

'bttnReplace

'

Me.bttnReplace.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.bttnReplace.Location =New System.Drawing.Point(224, 40)

Me.bttnReplace.Name ="bttnReplace"

Me.bttnReplace.Size =New System.Drawing.Size(96, 23)

Me.bttnReplace.TabIndex = 4

Me.bttnReplace.Text ="Replace"

'

'bttnFindNext

'

Me.bttnFindNext.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.bttnFindNext.Location =New System.Drawing.Point(336, 8)

Me.bttnFindNext.Name ="bttnFindNext"

Me.bttnFindNext.Size =New System.Drawing.Size(96, 23)

Me.bttnFindNext.TabIndex = 3

Me.bttnFindNext.Text ="Find Next"

'

'chkWord

'

Me.chkWord.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.chkWord.Location =New System.Drawing.Point(8, 96)

Me.chkWord.Name ="chkWord"

Me.chkWord.Size =New System.Drawing.Size(152, 24)

Me.chkWord.TabIndex = 7

Me.chkWord.Text ="Whole Word Only"

'

'bttnFind

'

Me.bttnFind.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.bttnFind.Location =New System.Drawing.Point(224, 8)

Me.bttnFind.Name ="bttnFind"

Me.bttnFind.Size =New System.Drawing.Size(96, 23)

Me.bttnFind.TabIndex = 2

Me.bttnFind.Text ="Find"

'

'txtSearchWord

'

Me.txtSearchWord.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.txtSearchWord.Location =New System.Drawing.Point(8, 8)

Me.txtSearchWord.Name ="txtSearchWord"

Me.txtSearchWord.Size =New System.Drawing.Size(200, 22)

Me.txtSearchWord.TabIndex = 0

Me.txtSearchWord.Text =""

'

'txtReplaceWord

'

Me.txtReplaceWord.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.txtReplaceWord.Location =New System.Drawing.Point(8, 40)

Me.txtReplaceWord.Name ="txtReplaceWord"

Me.txtReplaceWord.Size =New System.Drawing.Size(200, 22)

Me.txtReplaceWord.TabIndex = 1

Me.txtReplaceWord.Text =""

'

'chkCase

'

Me.chkCase.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.chkCase.Location =New System.Drawing.Point(8, 72)

Me.chkCase.Name ="chkCase"

Me.chkCase.Size =New System.Drawing.Size(152, 24)

Me.chkCase.TabIndex = 6

Me.chkCase.Text ="Case Sensitive"

'

'bttnReplaceAll

'

Me.bttnReplaceAll.Font =New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,CType(0,Byte))

Me.bttnReplaceAll.Location =New System.Drawing.Point(336, 40)

Me.bttnReplaceAll.Name ="bttnReplaceAll"

Me.bttnReplaceAll.Size =New System.Drawing.Size(96, 23)

Me.bttnReplaceAll.TabIndex = 5

Me.bttnReplaceAll.Text ="Replace All"

'

'FindForm

'

Me.AutoScaleBaseSize =New System.Drawing.Size(5, 13)

Me.ClientSize =New System.Drawing.Size(440, 125)

Me.Controls.Add(Me.chkWord)

Me.Controls.Add(Me.chkCase)

Me.Controls.Add(Me.bttnReplaceAll)

Me.Controls.Add(Me.bttnReplace)

Me.Controls.Add(Me.bttnFindNext)

Me.Controls.Add(Me.bttnFind)

Me.Controls.Add(Me.txtReplaceWord)

Me.Controls.Add(Me.txtSearchWord)

Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog

Me.MaximizeBox =False

Me.Name ="FindForm"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text ="Search and Replace"

Me.TopMost =True

Me.ResumeLayout(False)

EndSub

#EndRegion

' Dim RTFForm As Form

PrivateSub txtSearchWord_TextChanged(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles txtSearchWord.TextChanged, txtReplaceWord.TextChanged

found =False

If txtSearchWord.Text.Length > 0Then

bttnFind.Enabled =True

bttnFindNext.Enabled =True

If txtReplaceWord.Text.Length > 0Then

If foundThen

bttnReplace.Enabled =True

Else

bttnReplace.Enabled =False

EndIf

bttnReplaceAll.Enabled =True

Else

bttnReplace.Enabled =False

bttnReplaceAll.Enabled =False

EndIf

Else

bttnFind.Enabled =False

bttnFindNext.Enabled =False

bttnReplace.Enabled =False

bttnReplaceAll.Enabled =False

EndIf

EndSub

Public wordAtAsInteger

Public foundAsBoolean =False

PrivateSub bttnFind_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles bttnFind.Click, bttnFindNext.Click

If senderIs bttnFindThen

wordAt = 0

Else

wordAt += 1

EndIf

Dim srchModeAs RichTextBoxFinds

srchMode = SetSearchMode()

wordAt = Form1.RichTextBox1.Find(txtSearchWord.Text, wordAt, srchMode)

If wordAt = -1Then

MsgBox("Can't find any (more) matches", ,"Find and Replace")

found =False

ExitSub

EndIf

Form1.RichTextBox1.Select(wordAt, txtSearchWord.Text.Length)

Form1.RichTextBox1.ScrollToCaret()

found =True

If txtReplaceWord.Text.Length > 0Then

bttnReplace.Enabled =True

EndIf

EndSub

PrivateSub bttnReplaceAll_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles bttnReplaceAll.Click

Dim countAsInteger = 0

Dim srchModeAs RichTextBoxFinds

srchMode = SetSearchMode()

wordAt = 0

DoWhile wordAt <> -1

wordAt = Form1.RichTextBox1.Find(txtSearchWord.Text, wordAt, srchMode)

If wordAt = -1Then

Form1.RichTextBox1.ScrollToCaret()

MsgBox(count.ToString &" Replacements made", ,"Find and Replace")

ExitDo

Else

Form1.RichTextBox1.Select(wordAt, txtSearchWord.Text.Length)

' COMMENT OUT THE LINE BELOW TO GET JUST A COUNT

Form1.RichTextBox1.SelectedText = txtReplaceWord.Text

If txtSearchWord.Text.Length < txtReplaceWord.Text.LengthThen

wordAt = wordAt + txtSearchWord.Text.Length

Else

wordAt = wordAt + txtReplaceWord.Text.Length

EndIf

count += 1

EndIf

Loop

bttnFindNext.Enabled =False

bttnReplace.Enabled =False

bttnReplaceAll.Enabled =False

EndSub

PrivateSub bttnReplace_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles bttnReplace.Click

If Form1.RichTextBox1.SelectedText <>""Then

Form1.RichTextBox1.SelectedText = txtReplaceWord.Text

EndIf

bttnFind_Click(sender, e)

EndSub

Function SetSearchMode()As RichTextBoxFinds

Dim modeAs RichTextBoxFinds

If chkCase.Checked =TrueThen

mode = RichTextBoxFinds.MatchCase

Else

mode = RichTextBoxFinds.None

EndIf

If chkWord.Checked =TrueThen

mode = modeOr RichTextBoxFinds.WholeWord

Else

mode = modeOr RichTextBoxFinds.None

EndIf

SetSearchMode = mode

EndFunction

PrivateSub FindForm_FormClosing(ByVal senderAsObject,ByVal eAs System.Windows.Forms.FormClosingEventArgs)HandlesMe.FormClosing

found =False

EndSub

PrivateSub FindForm_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

If txtSearchWord.Text.Length > 0Then

bttnFind.Enabled =True

bttnFindNext.Enabled =True

If txtReplaceWord.Text.Length > 0Then

If foundThen

bttnReplace.Enabled =True

Else

bttnReplace.Enabled =False

EndIf

bttnReplaceAll.Enabled =True

Else

bttnReplace.Enabled =False

bttnReplaceAll.Enabled =False

EndIf

Else

bttnFind.Enabled =False

bttnFindNext.Enabled =False

bttnReplace.Enabled =False

bttnReplaceAll.Enabled =False

EndIf

EndSub

EndClass

TallDude at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Just a note. The example probably should have been added as

a class rather than a form. (I had to go back and blank out it's designer.vb.)

TallDude at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

I think my first post may have missed the whole point.

It was for counting specific words. If you want to count

just words, use the 'split' function.

PublicClass Form1

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

Dim TestArray()AsString = Split(RichTextBox1.Text," ")

MsgBox(TestArray.Length)

EndSub

EndClass

TallDude at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

thanks. I swear, microsoft really needs to work on their material on their site. It often confuses me more then helps.

Thanks for your help.

I will cobble and post how it went.

Some of the richtextboxes I am going to transfer data into, have limits on characters and or words.

So i need keep track.

thanks again.

CharlieRussell at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

Thx for the help

I got this to work

PrivateSub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

Dim TestArray() AsString = Split(Me.rtbDoc.Text, " ")

'Me.Label2.Text = (TestArray.Length)

Now am going to try get it to continuously update.

an endless loop perhaps or some such.

CharlieRussell at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

Charlie, try the text changed event

PrivateSub rtbDoc_TextChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles rtbDoc.TextChanged

' Set label2.text to blank in IDE

Dim TestArray()AsString = Split(Me.rtbDoc.Text," ")

Me.Label2.Text ="Words " &CStr((TestArray.Length - 1))

EndSub

Also, please feel free to mark useful replys as 'answered' in the forum. Thanks.

TallDude at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

thx, i could not get that approach to work.

Finally giving up on this new fangeled stuff, I did it using old vb, tinkered a bit and got that to work in VS 2005.

CharlieRussell at 2007-10-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...