Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even if the TextChanged event is usable for that i think the performance will be horrible if you have an already filled TextBox and simply enter another character.</p> <p>So to get this really to work smooth you need some additional efforts. Maybe restart a timer on every TextChanged event (1000ms), so the user won't be intercepted by entering something fast and start the word counting only when the timer event is fired up.</p> <p>Or think about putting the split and count algorithm into a background worker and maybe (or not) cancel the count if the user is going to enter something again into the box.</p> <h3>Update</h3> <p>Ok, here is an implementation example:</p> <pre><code>using System; using System.ComponentModel; using System.Windows.Forms; namespace WindowsFormsApplication { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { RestartTimer(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var textToAnalyze = textBox1.Text; if (e.Cancel) { // ToDo: if we have a more complex algorithm check this // variable regulary to abort your count algorithm. } var words = textToAnalyze.Split(); e.Result = words.Length; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled || e.Error != null) { // ToDo: Something bad happened and no results are available. } var count = e.Result as int?; if (count != null) { var message = String.Format("We found {0} words in the text.", count.Value); MessageBox.Show(message); } } private void timer1_Tick(object sender, EventArgs e) { if (backgroundWorker1.IsBusy) { // ToDo: If we already run, should we let it run or restart it? return; } timer1.Stop(); backgroundWorker1.RunWorkerAsync(); } private void RestartTimer() { if (backgroundWorker1.IsBusy) { // If BackgroundWorker should stop it's counting backgroundWorker1.CancelAsync(); } timer1.Stop(); timer1.Start(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload