I would want to search up, down, and match case if possible. Even links to get me started would be appreciated.
A:
Not sure about the up searching but as far as finding you can use something like this
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;
For matched case you can use String.ToLowerInvariant()
on both the search in text and the search for text otherwise String.Contains()
is case sensitive
searchIn.ToLowerInvariant().Contains(SearchFor.ToLowerInvariant())
Jeremy
2009-05-07 22:33:11
mmm that's a good start, I assume compareType is for searching up or down?
2009-05-07 22:36:32
+1
A:
You could use the "Find" method on the Rich Text Box itself.
If you setup a form with a check box for "Match Case" and a check box for "Search Up" and have added a property on your find form called ControlToSearch which takes in a RichTextBox control you could do something like the following:
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);
}
Clive
2009-05-08 11:12:39