I want to fetch each word individuall开发者_运维知识库y one by one in another string variable when I type something in a RichTextBox
. Which event will be fired on it and how will I get it?
Take a look at TextChanged event. Whenever text in control changes, this event fires. You can subscribe to it, and in event handler split your text to get each word individually, like this:
// subscribe to event elsewhere in your class
this.myRichTextBox.TextChanged += this.TextChangedHandler;
// ...
private void TextChangedHandler(object sender, EventArgs e)
{
string currentText = this.myRichTextBox.Text;
var words = currentText.Split(new [] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
// whatever else you want to do with words here
}
Edit:
If you want to get currently typed word, you could simply use IEnumerable.LastOrDefault:
var words = currentText.Split(new [] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
string currentlyTyped = words.LastOrDefault();
If you are worried about performance/user experience problems with splitting words everytime you type, you can just analyse last character and append it to some currentWord
:
// in your event handler
char newestChar = this.myRichTextBox.Text.LastOrDefault();
if (char.IsWhiteSpace(newestChar) || char.IsControl(newestChar))
{
this.currentWord = ""; // entered whitespace, reset current
}
else
{
this.currentWord = currentWord + newestChar;
}
Simply TextXhanged Event will be raised whenever you type somthing in the RichTextBox (surprise!)
ok this is what I would do in the TextChanged Event of the Richtextbox
string[] x = RichTextBox1.Text.Split(new char[]{ ' ' });
Iterate through the variable (sting array) x, and place the results where you need it. (like in a listview, a label etc.)
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.
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.
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.
Update
Ok, here is an implementation example:
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();
}
}
}
精彩评论