locked
C# How to add a word counter to a richtextbox

    Question

  • I am writing a word processor type program and I was wondering if there was any way to be able to count the words in the in a richtextbox control.
    Any ideas?
    Tuesday, November 24, 2009 4:45 PM

Answers

  •     private int GetWordCount()
        {
            // Add space to end of text to ensure the last word is counted.
            MatchCollection wordColl = Regex.Matches(richTextBox1.Text, @"[\W]+");
            return wordColl.Count;
        }
    

    • Proposed as answer by Tamer Oz Tuesday, November 24, 2009 5:59 PM
    • Marked as answer by Rudedog2Moderator Tuesday, November 24, 2009 6:30 PM
    Tuesday, November 24, 2009 5:07 PM
  • You can use richtextbox's textchanged event to update a label

    Here is a sample.

            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                MatchCollection wordColl = Regex.Matches(richTextBox1.Text, @"[\W]+");
                lblWords.Text=wordColl.Count.ToString();
            }
    Tuesday, November 24, 2009 5:58 PM

All replies

  •     private int GetWordCount()
        {
            // Add space to end of text to ensure the last word is counted.
            MatchCollection wordColl = Regex.Matches(richTextBox1.Text, @"[\W]+");
            return wordColl.Count;
        }
    

    • Proposed as answer by Tamer Oz Tuesday, November 24, 2009 5:59 PM
    • Marked as answer by Rudedog2Moderator Tuesday, November 24, 2009 6:30 PM
    Tuesday, November 24, 2009 5:07 PM
  • Do you need the exact word count or the average?

    I would just take the total characters in the control, and divide it by 5. 

    int averageWordCount = richTextBox.Text.Length / 5;

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, November 24, 2009 5:17 PM
    Moderator
  • where do you add this code?
    And are you sure it is in C#?
    Tuesday, November 24, 2009 5:45 PM

  • I'm pretty sure that Matthew posted C#.
    And I just checked my code.  It passed.


    As far as where to put it.   I dunno.  Can't see your screen.
    I would suggest in your form that has your RichTextBox that you mentioned.

    private int GetAverageWordCount(RichTextBox rtb)
    {
        return
    rtb.Text.Length / 5;   // return average word count, no error checking
    }


    That is a more complete example.  It is an entire method, not just a single line.
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, November 24, 2009 5:52 PM
    Moderator
  • Is there a way of displaying the word number in a label or something like that?
    Tuesday, November 24, 2009 5:55 PM
  • You can use richtextbox's textchanged event to update a label

    Here is a sample.

            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                MatchCollection wordColl = Regex.Matches(richTextBox1.Text, @"[\W]+");
                lblWords.Text=wordColl.Count.ToString();
            }
    Tuesday, November 24, 2009 5:58 PM
  • Is there anyway for you to ask questions more specific to what you have written?
    Posting an sample of it is most helpful to others you might wish to help you out.

    Otherwise, you force people to guess at what you have written.
    Been there, done that.  An exercise in frustration for both sides.
    An even bigger waste of time and effort better spent on others who need help, too.

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, November 24, 2009 6:01 PM
    Moderator
  • Sorry about how vague my question was, but you all really helped me so thank you.
    I will keep the idea about including some sample code in mind as well.

    This is the final piece of code that I am now using in my program.

            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                            System.Text.RegularExpressions.MatchCollection wordColl = System.Text.RegularExpressions.Regex.Matches(richTextBox1.Text,@"[\W]+");
                            label17.Text = wordColl.Count.ToString();
            }

    Thanks again everyone!
    Tuesday, November 24, 2009 6:14 PM