bool isSkip = false;
private void richTextBoxAdv_ContentChanged(object obj, ContentChangedEventArgs args)
{
if (isSkip)
return;
string wordToHighlight = "person";
TextPosition start = richTextBoxAdv.Document.GetTextPosition(richTextBoxAdv.Selection.Start.GetHierarchicalIndex);
TextSearchResults searchResults = richTextBoxAdv.FindAll(wordToHighlight, FindOptions.None);
if (searchResults != null && searchResults.Count > 0)
{
//Gets the last found word.
TextSearchResult lastResult = searchResults[searchResults.Count - 1];
if (!lastResult.Text.Equals(wordToHighlight))
{
//Reset the cursor.
richTextBoxAdv.Selection.Select(start, start);
return;
}
ChangeFormatAndReplaceWord(lastResult, wordToHighlight.ToUpper());
}
richTextBoxAdv.Focus();
|
string startIndex;
string endIndex;
bool isSkip = false;
/// <summary>
/// Intializes the instance for MainWindow.
/// </summary>
public MainWindow()
{
InitializeComponent();
richTextBoxAdv.ContentChanged += richTextBoxAdv_ContentChanged;
richTextBoxAdv.Focus();
}
/// <summary>
/// Change the format and replace the word.
/// </summary>
/// <param name="lastResult"></param>
/// <param name="wordToReplace"></param>
private void ChangeFormatAndReplaceWord(TextSearchResult lastResult, string wordToReplace)
{
if (lastResult == null)
return;
//Select the word.
richTextBoxAdv.Selection.Select(lastResult.Start, lastResult.End);
//Backup the highlight color.
HighlightColor highlightColor = HighlightColor.NoColor;
if (richTextBoxAdv.Selection.CharacterFormat.HighlightColor.HasValue)
highlightColor = richTextBoxAdv.Selection.CharacterFormat.HighlightColor.Value;
//Backup the bold value.
bool bold = richTextBoxAdv.Selection.CharacterFormat.Bold.HasValue ? richTextBoxAdv.Selection.CharacterFormat.Bold.Value : false;
//Set the flag to skip to avoid stack overflow, Since changing the selection format will apply the format to selected text and trigger the content changed event again.
isSkip = true;
//Change the format of selected word.
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = HighlightColor.Blue;
richTextBoxAdv.Selection.CharacterFormat.Bold = true;
//Replace the text.
lastResult.Replace(wordToReplace);
//Reset the cursor to last user edited position.
richTextBoxAdv.Selection.Select(richTextBoxAdv.Document.GetTextPosition(startIndex), richTextBoxAdv.Document.GetTextPosition(endIndex));
//Restore the selection format
richTextBoxAdv.Selection.CharacterFormat.HighlightColor = highlightColor;
richTextBoxAdv.Selection.CharacterFormat.Bold = bold;
isSkip = false;
}
/// <summary>
/// Called when the button is clicked.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
string wordToHighlight = "person";
startIndex = richTextBoxAdv.Selection.Start.GetHierarchicalIndex;
endIndex = richTextBoxAdv.Selection.End.GetHierarchicalIndex;
TextSearchResults searchResults = richTextBoxAdv.FindAll(wordToHighlight, FindOptions.None);
if (searchResults != null && searchResults.Count > 0)
{
//Gets the found word one by one.
for (int i = 0; i < searchResults.Count; i++)
{
TextSearchResult result = searchResults[i];
//if the text not equal, to skip the same word.
if (result.Text.Equals(wordToHighlight))
{
ChangeFormatAndReplaceWord(result, wordToHighlight.ToUpper());
i--;
}
}
}
//Reset the cursor to last user edited position.
richTextBoxAdv.Selection.Select(richTextBoxAdv.Document.GetTextPosition(startIndex), richTextBoxAdv.Document.GetTextPosition(endIndex));
richTextBoxAdv.Focus();
}
/// <summary>
/// Called when the rich text box adv content is changed.
/// </summary>
/// <param name="obj"></param>
/// <param name="args"></param>
private void richTextBoxAdv_ContentChanged(object obj, ContentChangedEventArgs args)
{
if (isSkip)
return;
startIndex = richTextBoxAdv.Selection.Start.GetHierarchicalIndex;
endIndex = richTextBoxAdv.Selection.End.GetHierarchicalIndex;
string wordToHighlight = "person";
TextPosition start = richTextBoxAdv.Document.GetTextPosition(richTextBoxAdv.Selection.Start.GetHierarchicalIndex);
TextSearchResults searchResults = richTextBoxAdv.FindAll(wordToHighlight, FindOptions.None);
if (searchResults != null && searchResults.Count > 0)
{
//Gets the last found word.
TextSearchResult lastResult = searchResults[searchResults.Count - 1];
if (!lastResult.Text.Equals(wordToHighlight))
{
//Reset the cursor to last user edited position.
richTextBoxAdv.Selection.Select(richTextBoxAdv.Document.GetTextPosition(startIndex), richTextBoxAdv.Document.GetTextPosition(endIndex));
return;
}
ChangeFormatAndReplaceWord(lastResult, wordToHighlight.ToUpper());
}
richTextBoxAdv.Focus();
|