You have to create a new form as a find and replace functionality. And then you have to write code for all the functionality.
For that you have to use RichTextBox.
See this code with RichTextBox, this is form coding;
原文地址:
http://www.eggheadcafe.com/community/aspnet/2/10046236/how-to-open-file-and-replace-dialog-box-.aspx
The find and replace form is supported with the frmReplace.cs class. The find class is defined in frmFind.cs but since it contains two subroutines (find and find next) which are also contained in the find and replace form, I will only discuss the code contained in the find and replace form.
The find and replace form supports four subroutines:
Find
Find Next
Replace
Replace All
The find subroutine is pretty straight forward, it will search the entire document for the first occurrence of the search term defined by the user on the form. It will search in one of two ways: With or without matching the case of the search term. Depending upon whether or not the user has checked the Match Case check box on the form, the application will search for the text using either the binary or text compare method. With the binary method, the search term must match exactly (including case), with the text compare method, the strings just need to match. The "StartPosition" integer value is set to the value returned by from the InStr call; InStr is passed the starting position of 1, the entire body of text contained in the rich text box control (as the article to search), the search term entered by the user, and the search compare method). InStr will return the index position of the found text if the text is in fact found, if nothing is found, it will return a zero. If the starting position value is zero, the user will be notified that the search term was not found, else, the application will highlight the found text in the document, pan to its location, and set the focus back to the main form (which in turn makes the highlighting visible to the user).
private
void
btnFind_Click(
object
sender, System.
EventArgs
e)
{
int
StartPosition;
CompareMethod SearchType;
if
(chkMatchCase.Checked ==
true
)
{
SearchType = CompareMethod.Binary;
}
else
{
SearchType = CompareMethod.Text;
}
StartPosition = InStr(1,
frmMain
.rtbDoc.Text, txtSearchTerm.Text, SearchType);
if
(StartPosition == 0)
{
MessageBox
.Show(
"String: "
+ txtSearchTerm.Text.ToString() +
" not found"
,
"No Matches"
,
MessageBoxButtons
.OK,
MessageBoxIcon
.Asterisk);
return
;
}
frmMain
.rtbDoc.Select(StartPosition - 1, txtSearchTerm.Text.Length);
frmMain
.rtbDoc.ScrollToCaret();
frmMain
.Focus();
}
The find next function works in a manner consistent with the find function; the only difference is that it sets the start position to the current position of the selection starting point within the document so that the find next function will not start at the beginning of the document each time it searches:
private
void
btnFindNext_Click(
object
sender, System.
EventArgs
e)
{
int
StartPosition =
frmMain
.rtbDoc.SelectionStart + 2;
CompareMethod SearchType;
if
(chkMatchCase.Checked ==
true
)
{
SearchType = CompareMethod.Binary;
}
else
{
SearchType = CompareMethod.Text;
}
StartPosition = InStr(StartPosition,
frmMain
.rtbDoc.Text, txtSearchTerm.Text, SearchType);
if
(StartPosition == 0)
{
MessageBox
.Show(
"String: "
+ txtSearchTerm.Text.ToString() +
" not found"
,
"No Matches"
,
MessageBoxButtons
.OK,
MessageBoxIcon
.Asterisk);
return
;
}
frmMain
.rtbDoc.Select(StartPosition - 1, txtSearchTerm.Text.Length);
frmMain
.rtbDoc.ScrollToCaret();
frmMain
.Focus();
}
The replace subroutine is quite simple, it merely tests to see if any text is selected and, if it is, it replaces with the replacement text entered into the form by the user, it then moves to the next occurrence of the search term if one exists:
private
void
btnReplace_Click(
object
sender, System.
EventArgs
e)
{
if
(
frmMain
.rtbDoc.SelectedText.Length != 0)
{
frmMain
.rtbDoc.SelectedText = txtReplacementText.Text;
}
int
StartPosition =
frmMain
.rtbDoc.SelectionStart + 2;
CompareMethod SearchType;
if
(chkMatchCase.Checked ==
true
)
{
SearchType = CompareMethod.Binary;
}
else
{
SearchType = CompareMethod.Text;
}
StartPosition = InStr(StartPosition,
frmMain
.rtbDoc.Text, txtSearchTerm.Text, SearchType);
if
(StartPosition == 0)
{
MessageBox
.Show(
"String: '"
+ txtSearchTerm.Text.ToString() +
"' not found"
,
"No Matches"
,
MessageBoxButtons
.OK,
MessageBoxIcon
.Asterisk);
return
;
}
frmMain
.rtbDoc.Select(StartPosition - 1, txtSearchTerm.Text.Length);
frmMain
.rtbDoc.ScrollToCaret();
frmMain
.Focus();
}
The replace all function is a little different in that it uses a the replace method to replace every instance of the search term with the replacement term throughout the entire body of text:
private
void
btnReplaceAll_Click(
object
sender, System.
EventArgs
e)
{
int
currentPosition =
frmMain
.rtbDoc.SelectionStart;
int
currentSelect =
frmMain
.rtbDoc.SelectionLength;
frmMain
.rtbDoc.Rtf = Replace(
frmMain
.rtbDoc.Rtf, Trim(txtSearchTerm.Text), Trim
(txtReplacementText.Text));
frmMain
.rtbDoc.SelectionStart = currentPosition;
frmMain
.rtbDoc.SelectionLength = currentSelect;
frmMain
.Focus();
}
Again, the frmFind.cs class is the same as the replace class with the exception being that it does not support the replace and replace all methods.
See this for more details;
http://www.c-sharpcorner.com/UploadFile/scottlysle/WordProcessor02042007234628PM/WordProcessor.aspx
http://www.csharpcorner.com/UploadFile/mgold/NotePadDotNet09042005163058PM/NotePadDotNet.aspx?ArticleID=ae1448cc-f5da-4dcd-b33c-776a9053788c&PagePath
=
Best Luck!!!!!!!!!!!!!
Sujit.
|