Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys , I am trying to make the label on my windows form project count the number of characters that are entered in the richTextbox .. something is wrong with my code below:
can you please help?


C#
  public int WordCount(RichTextBox rtb)
        {

            if (rtb.Text.Equals(""))
            {
                return 0;

            }

            else
            {
                string[] sWords = rtb.Text.Split(' ');

                int iWords = sWords.Length;
                return iWords;

            }


            }
    
            private void label1_Click(object sender, EventArgs e)
            {

            label1.Text = Convert.ToString(WordCount(rtbText));

        }
    }
}
Posted
Updated 27-Apr-13 2:14am
v3
Comments
Ravi Bhavnani 26-Apr-13 23:17pm    
What exactly do you mean by "something is wrong with my code"? Does it compile? If not, what errors does the compiler report? Does it cause an error when run? If so, what's the error?

/ravi
1Future 26-Apr-13 23:22pm    
yes sorry, it gives an error saying "The name rtbText does not exist in the current context"
[no name] 27-Apr-13 6:26am    
What is the name of your textbox and where is that defined?
1Future 27-Apr-13 7:08am    
i am using a richtexbox not a textbox..
[no name] 27-Apr-13 7:37am    
Hmmm gee.... RichTextBox or TextBox not see too much difference myself. And how does that answer the question?

rtbText should be rtb.Text rtb.

/ravi
 
Share this answer
 
v2
Comments
[no name] 27-Apr-13 1:35am    
But, the method accepts rich text box instance as in parameter..?!
Ravi Bhavnani 27-Apr-13 8:56am    
Yes, I corrected my typo.
Thank You guys I have solved it now, it was just as easy as this:


C#
private void richTextBox1_TextChanged(object sender, EventArgs e)
       {
           label1.Text = richTextBox1.Text.Length.ToString();
       }
 
Share this answer
 
Comments
Ravi Bhavnani 27-Apr-13 9:44am    
Your solution doesn't display the word count, just the total number of characters in the rich text box.
1Future 27-Apr-13 9:48am    
thats true....I apologise I must have ask the question wrong & I coded wrongly too, I did not want a "word count" but "character Count" ... and the solution "label1.Text = richTextBox1.Text.Length.ToString();" only counts up to 9 do you know why this is?
Ravi Bhavnani 27-Apr-13 9:52am    
It doesn't count up to only 9. Most likely, the AutoSize property of your label control is False, which limits the number of characters displayed. Change it True so that the label can grow to fit the size.
1Future 27-Apr-13 9:59am    
errm it was already true.. it just goes up to 9 then begins from 1 again
Ravi Bhavnani 27-Apr-13 16:25pm    
If you post your project I can take a look at it. There's no reason why the .Text.Length property should only "count up to 9".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900