110,533
社区成员
发帖
与我相关
我的任务
分享
private int FindNext(string szFindText, bool bMatchCase, bool bReverse)
{
if (szFindText == null)
return -1;
RichTextBoxFinds findOptions = RichTextBoxFinds.None;
int start = this.richTextBox1.SelectionStart;
int end = start;
if (bReverse)
{
if (start == 0)
return -1;
start = 0;
end = this.richTextBox1.SelectionStart;
findOptions |= RichTextBoxFinds.Reverse;
}
else
{
if (start == this.richTextBox1.TextLength)
return -1;
start += this.richTextBox1.SelectionLength;
end = this.richTextBox1.TextLength;
this.richTextBox1.SelectionStart = start;
}
this.richTextBox1.SelectionLength = 0;
if (bMatchCase)
{
findOptions |= RichTextBoxFinds.MatchCase;
}
return this.richTextBox1.Find(szFindText, start, end, findOptions);
}
private void button1_Click(object sender, EventArgs e)
{
int result = this.FindNext(this.textBox1.Text, 是否区分大小写, false);
if(result >= 0)
{
this.richTextBox1.SelectionColor = Color.Red; //把查到的字符设置为红色
}
}
注意:这里是从当前光标位置处开始查找的。
void Find(object sender, RoutedEventArgs e)
{
object wantedNode = stackPanel.FindName("dog");
if (wantedNode is TextBlock)
{
// Following executed if Text element was found.
TextBlock wantedChild = wantedNode as TextBlock;
wantedChild.Foreground = Brushes.Blue;
}
}