I have a search function with them. I can find a line inside my richtextbox from the very beginning of the text to the end. When I find the last match, we start from the beginning.
Here is the code:
#region Search
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
start = 0;
end = 0;
}
private void toolStripTextBoxSearch_Click(object sender, EventArgs e)
{
}
public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
{
int retVal = -1;
if (searchStart >= 0)
{
if (searchEnd > searchStart || searchEnd == -1)
{
indexOfSearchText = richTextBox.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
if (indexOfSearchText != -1)
{
retVal = indexOfSearchText;
}
}
}
return retVal;
}
private void buttonSearch_left_Click(object sender, EventArgs e)
{
}
private void buttonSearch_right_Click(object sender, EventArgs e)
{
int startindex = 0;
if (txtSearch.Text.Length > 0)
{
startindex = FindMyText(txtSearch.Text, start, richTextBox.Text.Length);
}
if (startindex < 0 )
{
if (stringfoundflag==1)
{
startindex = FindMyText(txtSearch.Text, 0, richTextBox.Text.Length);
}
else
{
MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
if (startindex >= 0)
{
stringfoundflag = 1;
int endindex = txtSearch.Text.Length;
richTextBox.Select(startindex, endindex);
start = startindex + endindex;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
start = 0;
indexOfSearchText = 0;
}
private void txtSearch_Enter(object sender, EventArgs e)
{
if (txtSearch.Text == "Search")
{
txtSearch.Text = "";
}
}
private void txtSearch_Leave(object sender, EventArgs e)
{
if (txtSearch.Text == "")
{
txtSearch.Text = "Search";
}
}
#endregion
I have two search buttons on the right and one on the left, on the right it works as a description at the beginning.
Question:
Is it possible to change the search function using the button from right to left using my solution or change the entire search function?
Example. Start with the first match and jump than before the last match, and then the next after the last, etc.
source
share