1

如果可能的话,我想向上、向下搜索并匹配大小写。即使是让我开始的链接也将不胜感激。

4

2 回答 2

1

您可以在富文本框本身上使用“查找”方法。

如果您设置了一个带有“Match Case”复选框和“Search Up”复选框的表单,并在您的查找表单上添加了一个名为 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);
}
于 2009-05-08T11:12:39.093 回答
0

不确定向上搜索,但只要找到你可以使用这样的东西

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())
于 2009-05-07T22:33:11.150 回答