您的位置:首页 > 其它

s thre anything default "Find and Replace" Dialog box

2010-08-31 18:04 549 查看

Is thre anything default "Find and Replace" Dialog box

Posted by Eswaran Radhakrishnan
in C# .NET

05-Aug-08 03:15 AM

Hi all,

Is there anything defualut "Find and Replace" dialog box ? I need to open the "Find and Replace" defalt dialog box when I am clicking the form.

Thanks

R. Eswaran.

Breaking News Download over 60 FREE Controls from DevExpress. No royalties, just free controls!

Reply
Reply Using Power Editor

Ads by Google
C# Code Review Tool
- Free Visual Studo addin for code review and comparison - devart.com
Openmp C#
- Great overview and code samples for OpenMP. Find a better way! - drdobbs.com
Objective Systems ASN1C
- ASN.1 to C/C++, Java, C# compiler with XML/XSD support. Free Eval. - obj-sys.com
Complete .NET Protection
- Code Encryption, Anti-Decompiling, Obfuscation,Licensing and much more - eziriz.com
HOOPS 3D Graphics Toolkit
- Used by 200+ Industry Leaders Compare your App to Our Demo! - techsoft3d.com


No

Sujit Patil
replied to Eswaran Radhakrishnan



05-Aug-08 03:21 AM

I think there is no such a dialog box . 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;

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.

Reply
Reply Using Power Editor



u should create yr customize dialog box

Web star
replied to Eswaran Radhakrishnan



05-Aug-08 03:32 AM

This article demonstrates how to use regular expressions and [code]RegEx

class (
System.Text.RegularExpressions

namespace) to build the find and replace functionality found in most of the text editors and word processors. The functionalities like whole word search or case sensitive/insensitive search can be implemented much easier using Regular Expressions than using any other methods. To demonstrate further, I included a wild card search. In wild card search (as you know), you can use * to represent a group of characters and ? to represent a single character. For example if you enter a* and check "Use wildcards" checkbox, all words starting with a are selected. In addition to this, I have included a regular expression search, which helps you if you are a regular expression freak.

//
Declare the regex and match as class level variables

//
to make happen find next

private
Regex regex;

private
Match match;

//
variable to indicate finding first time

//
or is it a find next

private
bool
isFirstFind = true
;

private
void
replaceAllButton_Click(object
sender, EventArgs e)

{

Regex replaceRegex = GetRegExpression();

String
replacedString;

//
get the current SelectionStart

int
selectedPos = contentTextBox.SelectionStart;

//
get the replaced string

replacedString = replaceRegex.Replace(contentTextBox.Text, replaceTextBox.Text);

//
Is the text changed?

if
(contentTextBox.Text != replacedString)

{

//
then replace it

contentTextBox.Text = replacedString;

MessageBox.Show("
Replacements are made. "
, Application.ProductName,

MessageBoxButtons.OK, MessageBoxIcon.Information);

//
restore the SelectionStart

contentTextBox.SelectionStart = selectedPos;

}

else
//
inform user if no replacements are made

{

MessageBox.Show(String
.Format("
Cannot find '{0}'. "
, searchTextBox.Text),

Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

}

contentTextBox.Focus();

}

The
GetRegExpression

function returns an instance of
Regex

class, depending on text entered by user in the form and checkboxes selected. Once we get this instance, we can use
Replace

method to make the replacements. Then our job is done.

Now let's examine the
GetRegExpression

function. This function is called from most of the methods in this article.



Collapse

//
This function makes and returns a RegEx object

//
depending on user input

private
Regex GetRegExpression()

{

Regex result;

String
regExString;

//
Get what the user entered

regExString = searchTextBox.Text;

if
(useRegulatExpressionCheckBox.Checked)

{

//
If regular expressions checkbox is selected,

//
our job is easy. Just do nothing

}

//
wild cards checkbox checked

else
if
(useWildcardsCheckBox.Checked)

{

regExString = regExString.Replace("
*"
, @"
/w*"
); //
multiple characters wildcard (*)

regExString = regExString.Replace("
?"
, @"
/w"
); //
single character wildcard (?)

//
if wild cards selected, find whole words only

regExString = String
.Format("
{0}{1}{0}"
, @"
/b"
, regExString);

}

else

{

//
replace escape characters

regExString = Regex.Escape(regExString);

}

//
Is whole word check box checked?

if
(matchWholeWordCheckBox.Checked)

{

regExString = String
.Format("
{0}{1}{0}"
, @"
/b"
, regExString);

}

//
Is match case checkbox checked or not?

if
(matchCaseCheckBox.Checked)

{

result = new
Regex(regExString);

}

else

{

result = new
Regex(regExString, RegexOptions.IgnoreCase);

}

return
result;

}

[/code]

From the code listing above, it is clear that the
GetRegExpression

function does most of the important jobs.

This is all what we need to do to implement the Replace All functionality. Now let's examine how the Find functionality is implemented.



Collapse

//
Click event handler of find button

private
void
findButton_Click(object
sender, EventArgs e)

{

FindText();

}

//
finds the text in searchTextBox in contentTextBox

private
void
FindText()

{

//
Is this the first time find is called?

//
Then make instances of RegEx and Match

if
(isFirstFind)

{

regex = GetRegExpression();

match = regex.Match(contentTextBox.Text);

isFirstFind = false
;

}

else

{

//
match.NextMatch() is also ok, except in Replace

//
In replace as text is changing, it is necessary to

//
find again

//
match = match.NextMatch();

match = regex.Match(contentTextBox.Text, match.Index + 1
);

}

//
found a match?

if
(match.Success)

{

//
then select it

contentTextBox.SelectionStart = match.Index;

contentTextBox.SelectionLength = match.Length;

}

else
//
didn't find? bad luck.

{

MessageBox.Show(String
.Format("
Cannot find '{0}'.   "
, searchTextBox.Text),

Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

isFirstFind = true
;

}

}


From the click event handler of
findButton

, the
FindText

method is called. The
FindText

is called from the Replace also. That's why I made it a separate function instead of writing the code in event handler itself.

Now the only functionality that remains to explore is Replace. Let's complete that too.

//
Click event handler of replaceButton

private
void
replaceButton_Click(object
sender, EventArgs e)

{

//
Make a local RegEx and Match instances

Regex regexTemp = GetRegExpression();

Match matchTemp = regexTemp.Match(contentTextBox.SelectedText);

if
(matchTemp.Success)

{

//
check if it is an exact match

if
(matchTemp.Value == contentTextBox.SelectedText)

{

contentTextBox.SelectedText = replaceTextBox.Text;

}

}

FindText();

}


So, before winding up code listing, a small task is pending. What to do with
isFirstFind

variable? We declared this as a private variable and checked its value in
FindText

to see whether the user is pressing the Find button for the first time or not. Then we set its value to false, if it is first time so that next find will be considered as find next. Again, we set its value to true, if no match is found for a search. Is this enough? Definitely, no. The problem is how we can find that the user completed a search and when we can start from the beginning again? The method I followed is if the
searchTextBox

or any of the checkboxes is changed, it initializes a new search. This may not be the best approach, but hope it satisfies most of the users. See code listing below.

//
TextChanged event handler of searchTextBox

//
Set isFirstFind to true, if text changes

private
void
searchTextBox_TextChanged(object
sender, EventArgs e)

{

isFirstFind = true
;

}

//
CheckedChanged event handler of matchWholeWordCheckBox

//
Set isFirstFind to true, if check box is checked or unchecked

private
void
matchWholeWordCheckBox_CheckedChanged(object
sender, EventArgs e)

{

isFirstFind = true
;

}

//
CheckedChanged event handler of matchCaseCheckBox

//
Set isFirstFind to true, if check box is checked or unchecked

private
void
matchCaseCheckBox_CheckedChanged(object
sender, EventArgs e)

{

isFirstFind = true
;

}

//
CheckedChanged event handler of useWildcardsCheckBox

//
Set isFirstFind to true, if check box is checked or unchecked

private
void
useWildcardsCheckBox_CheckedChanged(object
sender, EventArgs e)

{

isFirstFind = true
;

}


Reply
Reply Using Power Editor



Check this

san san
replied to Eswaran Radhakrishnan

05-Aug-08 05:00 AM

Hi

As far as I know, there is no built-in FindAndReplace dialog box for common windows application. You need to create it yourself.

Go through this link

http://www.codeproject.com/KB/cs/FindAndReplace.aspx

cheers

Reply
Reply Using Power Editor



Use this...

Atul Shinde
replied to Eswaran Radhakrishnan

05-Aug-08 07:27 AM

Hi Use this Link : http://www.codeproject.com/KB/cs/FindAndReplace.aspx 
原文地址: http://www.eggheadcafe.com/community/aspnet/2/10046573/is-thre-anything-default-find-and-replace-dialog-box.aspx[/code] 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐