I am new c#. I try to a notepad.
But I can not create find function in my notepad. I want to find string and highlight it.
I used textbox, not used richtextbox for notepad.
thank you.
note : soryy for my bad english.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Alex SmithPosted Sep 2, 2014, 3:40 AM
http://stackoverflow.com/questions/17101889/how-to-create-find-in-notepad-in-c-sharp
ghasem dehPosted Aug 29, 2014, 11:54 AM
For textBoxt :
private void findButton_Click(object sender, EventArgs e) { int count = 0; string keyword = keywordTextbox.Text.Trim();//getting given searching string int startPosition = 0; //initializing starting position to search int endPosition = 0; int endArticle = articleRichTextbox.Text.Length;//finding total number of character into the article for (int i = 0; i<endArticle ; i =startPosition )//creating a loop until ending the article { if (i == -1) //if the loop goes to end of the article then stop { break; } startPosition = articleRichTextbox.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);//using find method get the begining position when find the searching string if (startPosition >= 0) //if match the string //if don't match the string then it return -1 { count++;//conunting the number of occuerence or match the search string articleRichTextbox.SelectionColor = Color.Blue;//coloring the matching string in the article endPosition = keywordTextbox.Text.Length; startPosition = startPosition + endPosition;//place the starting position at the next word of previously matching string to continue searching. } } if (count == 0)//if the givn search string don't match at any time { MessageBox.Show("No Match Found!!!"); } numberofaccurTextbox.Text = count.ToString();//show the number of occurence into the text box }
Link
For richTextBox :
private void button1_Click(object sender, EventArgs e)
{
try
{
int count = 0;
string keyword = textBox1.Text.Trim();
int startPosition = 0;
int endPosition = 0;
int endArticle = richTextBox1.Text.Length;
for (int i = 0; i < endArticle; i = startPosition)
{
if (i == -1)
{
break;
}
startPosition = richTextBox1.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);
if (startPosition >= 0)
{
count++;
richTextBox1.SelectionBackColor = Color.PaleGoldenrod;
endPosition = textBox1.Text.Length;
startPosition = startPosition + endPosition;
}
}
if (count == 0)
{
MessageBox.Show("???? ???? ??? ???? ???","Find",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
MessageBox.Show("????? : " + count + " ???? ???? ??", "Find", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("????? ?? ???? ? ???? ????? ???? ????","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private void textBox1_MouseClick(object sender, MouseEventArgs e) //search textBox
{
int count = 0;
string keyword = textBox1.Text.Trim();
int startPosition = 0;
int endPosition = 0;
int endArticle = richTextBox1.Text.Length;
for (int i = 0; i < endArticle; i = startPosition)
{
if (i == -1)
{
break;
}
startPosition = richTextBox1.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);
if (startPosition >= 0)
{
count++;
richTextBox1.SelectionBackColor = Color.Azure;
endPosition = textBox1.Text.Length;
startPosition = startPosition + endPosition;
}
}
}
}