我尝试了“查找下一个”的代码richtextbox,但似乎它无法让我找到找到的下一个字符串。即使有更多字符串,它也会停在第一个字符串处。这是代码示例:
void Button12_Click(object sender, EventArgs e)
{
string searchText = textBox2.Text ;
int findPos = 0;
try
{
string s = textBox2.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos, s.Length);
findPos += textBox2.Text.Length +1;
//i = richTextBox1.Find(s, i + s.Length, RichTextBoxFinds.None);
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
每次按下按钮时,您都会从头开始搜索。如果您将变量设置为findPos实例方法,它将在您期望的位置进行搜索。
// make this a variable in the class instead of the method
private int findPos = 0;
void Button12_Click(object sender, EventArgs e)
{
string searchText = textBox2.Text ;
try
{
string s = textBox2.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos, s.Length);
findPos += textBox2.Text.Length +1;
//i = richTextBox1.Find(s, i + s.Length, RichTextBoxFinds.None);
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
如果更改文本框中的文本,请不要忘记重置 findPos。
| 归档时间: |
|
| 查看次数: |
1632 次 |
| 最近记录: |