diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 2f11f18..66a3237 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -655,6 +655,7 @@ private void InitializeComponent() this.richTextBox1.Size = new System.Drawing.Size(1600, 724); this.richTextBox1.TabIndex = 2; this.richTextBox1.Text = ""; + this.richTextBox1.SelectionChanged += new System.EventHandler(this.richTextBox1_SelectionChanged); this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged); // // saveFileDialog1 diff --git a/Form1.cs b/Form1.cs index 9781395..5272161 100644 --- a/Form1.cs +++ b/Form1.cs @@ -100,7 +100,22 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e) Application.Exit(); } } - + + int WordCount(string text) + { + int count = 0; + for (int i = 0; i < text.Length; ++i) + { + if (!char.IsWhiteSpace(text[i])) + { + ++count; + while (++i < text.Length && !char.IsWhiteSpace(text[i])) + ; + } + } + return count; + } + private void richTextBox1_TextChanged(object sender, EventArgs e) { if (TextHasChanged == false) @@ -109,14 +124,12 @@ private void richTextBox1_TextChanged(object sender, EventArgs e) TextHasChanged = true; } - int Count = System.Text.RegularExpressions.Regex.Matches(richTextBox1.Text, @"[\S]+").Count; + int Count = WordCount(richTextBox1.Text); wordCountToolStripStatusLabel.Text = Count.ToString() + " word"; if (Count > 1) { wordCountToolStripStatusLabel.Text += "s"; } - - ChangePositionToolStripStatusLabel(); } private void ExitToolStripMenuItem_Click(object sender, EventArgs e) @@ -711,5 +724,10 @@ private void replaceAllToolStripMenuItem_Click(object sender, EventArgs e) else MessageBox.Show("Cannot find '" + FindTextString + "'", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } + + private void richTextBox1_SelectionChanged(object sender, EventArgs e) + { + ChangePositionToolStripStatusLabel(); + } } }