C# 创建查找下一个函数
如何创建查找下一个函数?
从我当前的代码来看,如果在 richTextBoxBrowsing 中找到 textBoxSearch 中的单词,则该单词将在 richTextBoxBrowsing 内突出显示。
但如果找到多个相同的单词,我只能看到第一个。因此,我想按F3按钮查找下一个单词,它会被一个一个突出显示,直到richTextBoxBrowsing结束。
提前致谢!
String s1 = textBoxSearch.Text.ToLower();
int startPos = richTextBoxBrowsing.Find(s1);
int length = s1.Length;
if (startPos > -1)
{
MessageBox.Show("Word found!");
richTextBoxBrowsing.Focus();
richTextBoxBrowsing.Select(startPos, length);
}
else
{
MessageBox.Show("Word not found!");
}
How can I create a find next function?
From my current codes, if a word from textBoxSearch is found richTextBoxBrowsing, the word would be highlighted inside the richTextBoxBrowsing.
But if there are more than 1 of the same word found, I can only see the first one. Hence, I would like to press the button F3 to find the next word and it would be highlighted one by one until the end of richTextBoxBrowsing.
Thanks in advance!
String s1 = textBoxSearch.Text.ToLower();
int startPos = richTextBoxBrowsing.Find(s1);
int length = s1.Length;
if (startPos > -1)
{
MessageBox.Show("Word found!");
richTextBoxBrowsing.Focus();
richTextBoxBrowsing.Select(startPos, length);
}
else
{
MessageBox.Show("Word not found!");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
诀窍是保留最后一个已知索引(即您为
startPos
获得的最后一个值) - 也许在表单级字段中,然后您可以使用:(其中
lastIndex< /code> 为 -1 将导致它从头开始)
The trick is to keep hold of the last known index (i.e. the last value you got for
startPos
) - perhaps in a form-level field, then you can use:(where a
lastIndex
of -1 will cause it to start at the beginning)您必须保存先前搜索的状态,例如记住先前找到的项目的索引。每当搜索字符串发生更改时,您都将起始索引重置为 -1。
You'd have to save the state of the previous search, for instance the remember the index of the previously found item. Whenever the search string changes, you reset the starting index to -1.
这是我所拥有的“查找下一个”功能。它是在 VB.net 中,因为我目前正在开发一个 TAFE 项目,但您可以轻松地将其转换为 C#。这对我来说有奇效。
我有一个名为“RichTextBox1”的主富文本框,其中包含文本,然后有一个名为“ToolStripSearchTextBox”的文本框,我在其中输入要搜索的内容,还有一个名为“ToolStripButton2”的按钮,单击时调用方法“FindNext_Click()”。
由于“RichTextBoxFinds.None”,此“查找下一个”功能不区分大小写。您可以随意更改它。
Here's what I've got for a "Find Next" function. It's in VB.net as I'm currently working on a TAFE project but you can easily convert it to C#. It works wonders for me.
I have a main richtextbox called 'RichTextBox1' where the text is, then I have a textbox called 'ToolStripSearchTextBox' where I input what I want to search and a button called 'ToolStripButton2' that calls the method 'FindNext_Click()' when clicked.
This "Find Next" function is not case-sensitive due to 'RichTextBoxFinds.None'. Feel free to change that as you wish.