如果可能的话,我想搜索上下,匹配案例。即使是让我入门的链接也会受到赞赏。

有帮助吗?

解决方案

不确定向上搜索,但发现你可以使用这样的东西

int selStart = ControltoSearch.SelectionStart;
int selLength = ControltoSearch.SelectionLength;
int newLength = SearchFor.Length;

int newStart = searchIn.IndexOf(SearchFor, selStart + selLength, compareType);

ControltoSearch.SelectionStart = newStart >= 0 ? newStart : 0;
ControltoSearch.SelectionLength = newLength;
ControltoSearch.ScrollToCaret();
ControltoSearch.Focus();

return newStart;

对于匹配的情况,您可以在文本搜索和搜索文本上使用String.ToLowerInvariant(),否则String.Contains()区分大小写

searchIn.ToLowerInvariant().Contains(SearchFor.ToLowerInvariant())

其他提示

您可以使用<!>“查找<!>”;富文本框本身的方法。

如果您使用<!>匹配复选框设置表单;匹配案例<!>“;以及<!>“;向上搜索<!>”复选框;并在您的查找表单上添加了一个名为ControlToSearch的属性,该属性接受RichTextBox控件,您可以执行以下操作:

RichTextBoxFinds options = RichTextBoxFinds.None;

int from = ControlToSearch.SelectionStart;
int to = ControlToSearch.TextLength - 1;

if (chkMatchCase.Checked)
{
    options = options | RichTextBoxFinds.MatchCase;
}
if (chkSearchUp.Checked)
{
    options = options | RichTextBoxFinds.Reverse;
    to = from;
    from = 0;
}

int start = 0;
start = ControlToSearch.Find(txtSearchText.Text, from, to, options);

if (start > 0)
{
    ControlToSearch.SelectionStart = start;
    ControlToSearch.SelectionLength = txtSearchText.TextLength;
    ControlToSearch.ScrollToCaret();
    ControlToSearch.Refresh();
    ControlToSearch.Focus();
}
else
{
    MessageBox.Show("No match found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top