1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
public void SelectText(string wordToSearch, int timer)
{
TextRange tr = new TextRange(txtBoxContent.Document.ContentStart, txtBoxContent.Document.ContentEnd);
int startPosition = tr.Text.IndexOf(wordToSearch);
if (startPosition == -1)
{
MessageBox.Show("The text has not been found");
}
else
{
int lines = GetLinesCount(tr.Text, startPosition);
TextPointer startRtb = txtBoxContent.Document.ContentStart;
TextPointer start = startRtb.GetPositionAtOffset(startPosition + lines);
TextPointer end = start.GetPositionAtOffset(wordToSearch.Length);
if (timer == 0)
{
txtBoxContent.Selection.Select(start, end);
}
for (int i = 0; i < timer; i++)
{
startRtb = end;
tr.Select(startRtb, txtBoxContent.Document.ContentEnd);
startPosition = tr.Text.IndexOf(wordToSearch);
if (startPosition == -1)
{
MessageBox.Show("The text has not been found");
return;
}
start = startRtb.GetPositionAtOffset(startPosition + lines);
end = start.GetPositionAtOffset(wordToSearch.Length);
txtBoxContent.Selection.Select(start, end);
}
txtBoxContent.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, txtBoxContent.SelectionBrush);
txtBoxContent.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
txtBoxContent.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
txtBoxContent.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Yellow));
}
} |
Partager