在系统自带的记事本有一个功能是查找,他先查找第一个,然后我们接着按查找,他会查找文本中的第二个要查找的位置,然后将那个查找的字在文本中以选中的方式显示,我也想实现这样的效果,但是我只做出了查找第一个,无法让他查找第二个,请问怎么实现那个查找第二要查找的字符的方法,下面是我查找第一个的代码
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.SelectionColor = Color.Red;
int a = this.richTextBox1.Text.IndexOf(textBox1.Text);
this.richTextBox1.SelectionStart = a;
this.richTextBox1.SelectionLength = this.textBox1.Text.Length;
}
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.SelectionColor = Color.Red;
int a = this.richTextBox1.Text.IndexOf(textBox1.Text);
this.richTextBox1.SelectionStart = a;
this.richTextBox1.SelectionLength = this.textBox1.Text.Length;
}
{
this.richTextBox1.SelectionColor = Color.Red;
int a = this.richTextBox1.Text.IndexOf(textBox1.Text, current);
this.richTextBox1.SelectionStart = a;
this.richTextBox1.SelectionLength = this.textBox1.Text.Length;
current = a + 1;
}
那怎么取消选中状态或者怎样验证他依然是被选中的,接上面的代码,
当被选中的时候字体显示红色,但当我在文本框中的随便点击,红色字体并没有退去,那是代表字体依然是被选中状态的意思吗?
如果是的话,怎么取消他的选中状态?
如果是由于上面的那句this.richTextBox1.SelectionColor = Color.Red;,而使字体选中状态被取消了,但依然不能是颜色改变回来的话,那又怎么验证字体是处于被选中还是没有被选中状态呢?
谢谢
int 当前 = 0;
int 之前 = 0;
int 之前Length = 0;
int 位置 = 0; private void button1_Click(object sender, EventArgs e)
{
if (this.richTextBox1.Text.Contains(this.textBox1.Text) == true)
{
richTextBox1.Focus(); 当前 = this.richTextBox1.Text.IndexOf(this.textBox1.Text, 位置);
之前Length = this.textBox1.TextLength; if (位置 != 0)
{
this.richTextBox1.SelectionStart = 之前;
this.richTextBox1.SelectionLength = 之前Length;
this.richTextBox1.SelectionColor = Color.Black;
} this.richTextBox1.SelectionStart = 当前;
this.richTextBox1.SelectionLength = this.textBox1.TextLength;
this.richTextBox1.SelectionColor = Color.Red;
之前 = richTextBox1.SelectionStart;
位置 += 1; }
对不起,我的表达能力并非很好,我想实现的是我现在选中了文字,现在想把他们选中的那个状态取消掉,变成没有选中的状态,
就像记事本那样的,查找第一次,第一个文字就会被选中,查找2次,前一次选中状态就会被取消掉,而去选中第二个文字
请问实现如何