The WinForms Notepad Project: Word Count (Premium)

I was looking at Microsoft Word to see whether there were any obvious features that made sense for .NETpad. The first idea I had was to add a zoom slider like that found in the bottom right of the Word application window. But because the Windows Forms StatusStrip control only supports labels, progress bars, drop-down buttons, and split buttons---and not any kind of slider control---that wouldn’t work.

And then I saw word count, which appears as a label in Word’s status bar. Word count would be very useful in a text editor. And it seemed like the type of thing that would be fairly easy to implement.

If you Google this, as I did, you’ll find a very common solution that relies on counting the number of spaces in a string of text. But this method is broken: If you just type a single SPACE, it will report that your document has one word. And if you use double spaces between words, the count gets screwed up.

But then I came across this solution, which is based on Visual Basic’s support for regular expressions. And after tinkering around with it a bit, I was able to reduce it to just a short bit of code that seems to work perfectly. Nice.

First, we need to find a spot for our Word Count label in the status bar. To do that, open Form1.vb [Design], right-click on ZoomToolStripStatusLabel in the status bar, and choose Insert > StatusLabel from the popup that appears.

Then, make the following property changes to the status label in the Properties pane:

Name: WordCountToolStripStatusLabel
AutoSize: False
BorderSides: Left
Size: 350, 52
Text: “0 words” (no quotes)
TextAlign: MiddleLeft

Since the word count will potentially need to change every time the user changes the text in the text box, double-click TextBox1 to display the TextBox1_TextChanged event handler in Form1.vb. Right now, this event handler contains the following code:
If TextHasChanged = False Then
    Me.Text = "*" + Me.Text
    TextHasChanged = True
End If
ChangePositionToolStripStatusLabel()
Now, add the following line of code between End If and ChangePositionToolStripStatusLabel():
WordCountToolStripStatusLabel.Text = System.Text.RegularExpressions.Regex.Matches(TextBox1.Text, "\S+").Count.ToString + " words"
This works great, but there is one problem: If there’s only one word in the text box, the word count label will read as “1 words.” So, we’ll need to fix that.

Replace that line of code you added with the following:
Dim Count As Integer = System.Text.RegularExpressions.Regex.Matches(TextBox1.Text, "\S+").Count
WordCountToolStripStatusLabel.Text = Count.ToString + " word"
If Count > 1 Then
    WordCountToolStripStatusLabel.Text += "s"
End If
And there you go. Pretty slick.

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC