OK people, I am creating a class that basically has to interact with a RichTextBox in c# .net. One of its primary functions is to detect if the user typed a special word, if it finds a match it has to change the word’s color (like in word processor).
The thing is that I am not sure how to go about doing it in an efficient way cause I was thinking about doing a search every time the text in the RTB (RichTextBox) is modified but it would be too inefficient to search the whole RTB every time the user types a new word (considering that the text in the RTB is long).
I found a way to get the last character typed in the RTB and determine if a word was formed and then check if it was a special word, but the problem is that if the RTB’s text is modified by functions or by pasting text then it would not work. So, I have to take into consideration that the RTB’s text can be modified in many ways.
So, would it be efficient to do a full search in the RTB every time the text is changed to find the special words? (taking into account that the text on it may be long) or is there any other approach you guys have in mind?
EDIT:—————————-
Well in case somebody is interested I found a way to solve the problem.
I used an integer in my class to keep track of the current selection position (the current or last know position of the cursor in the RTB) by updating it each time the selection was changed in the RTB with the event RichTextBox_SelectionChanged. Like this:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
LastCursorPosition = CurrentCursorPosition;
CurrentCursorPosition = richTextBox1.SelectionStart;
CursorsDifferences = CurrentCursorPosition - LastCursorPosition;
}
this will ensure that CurrentCursorPosition will be updated every time the user types a character or modifies the text in the RTB and LastCursorPosition will hold, well do I have to say it? Then the differences in the cursors positions will be saved on CursorsDifferences that way if the user pasted something into the RTB CursorsDifferences will tell you the length of the string inserted. Based on that you can do the search from
LastCursorPosition to CurrentCursorPosition.
NOTE:
Searching from LastCursorPosition to CurrentCursorPosition might not be accurate because LastCursorPosition might NOT be the beginning of a word therefore you have to find the previous ‘ ‘(whitesapce) or ‘\n’ (newline) and get the position of the character in front of a ‘ ‘ or ‘\n’ and do the search based on the position, same thing applies to CurrentCursorPosition.
Well in case somebody is interested I found a way to solve the problem.
I used an integer in my class to keep track of the current selection position (the current or last know position of the cursor in the RTB) by updating it each time the selection was changed in the RTB with the event RichTextBox_SelectionChanged. Like this:
this will ensure that CurrentCursorPosition will be updated every time the user types a character or modifies the text in the RTB and LastCursorPosition will hold, well do I have to say it? Then the differences in the cursors positions will be saved on CursorsDifferences that way if the user pasted something into the RTB CursorsDifferences will tell you the length of the string inserted. Based on that you can do the search from LastCursorPosition to CurrentCursorPosition.
NOTE: Searching from LastCursorPosition to CurrentCursorPosition might not be accurate because LastCursorPosition might NOT be the beginning of a word therefore you have to find the previous ‘ ‘(whitesapce) or ‘\n’ (newline) and get the position of the character in front of a ‘ ‘ or ‘\n’ and do the search based on the position, same thing applies to CurrentCursorPosition.