您的位置:首页 > 运维架构

How to Open "File And Replace" Dialog Box ?

2010-08-31 18:02 621 查看

How to Open "File And Replace" Dialog Box ?

Posted by Eswaran Radhakrishnan
in C# .NET

01-Aug-08 08:03 AM

Hi all,

I have to open the "FileAndReplace" Dialog box in Windows application and I need to use the same funanlities in my windows form. If i click the replace button it shold be replace even if the text appers in textbox.

any help..

Thanks

R. Eswaran.

Breaking News Sisulizer 2010 improves machine translation of software

Reply
Reply Using Power Editor

Ads by Google
C# Code Review Tool
- Free Visual Studo addin for code review and comparison - devart.com
Objective Systems ASN1C
- ASN.1 to C/C++, Java, C# compiler with XML/XSD support. Free Eval. - obj-sys.com
PowerBasic vs VisualBasic
- Faster. No Run-Times. No Bloat! CGI, Macros, ASM, Reg Expressions - powerbasic.com
Complete .NET Protection
- Code Encryption, Anti-Decompiling, Obfuscation,Licensing and much more - eziriz.com
Protect your.NET Software
- BitHelmet Obfuscator for .NET Obfuscation that just works! - bithelmet.com


See this to Open Find And Replace

Sujit Patil
replied to Eswaran Radhakrishnan



01-Aug-08 08:14 AM

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.

Reply
Reply Using Power Editor



hi Eswaran.

pravin kumar S
replied to Eswaran Radhakrishnan

01-Aug-08 08:40 AM

http://msdn.microsoft.com/en-us/library/aa302025(VS.71).aspx

go though this link

Reply
Reply Using Power Editor



reply

Binny ch
replied to Eswaran Radhakrishnan



01-Aug-08 09:47 AM

static
public
bool
FindNext(string
searchString, bool
caseSensitive,

TextBox txtControl, bool
useRegularExpression)

{

// track the current search string

CurrentSearchString = searchString;

Regex regularExpression = null
;

// get the length of the search string

int
searchLength = searchString.Length;

// handle case sensitive strings separately

if
(caseSensitive)

{

if
(useRegularExpression)

{

// we are using regular expressions, create a RegularExpression object

try

{

regularExpression = new
Regex(searchString);

}

catch
(Exception ex)

{

MessageBox.Show("Invalid Regular expression");

return
false
;

}

// Now match the regular expression

Match match = regularExpression.Match(txtControl.Text, _currentIndex);

// if we successfully matched, get the index location of the match inside

// the textbox control and the length of the match

if
(match.Success)

{

_currentIndex = match.Index;

searchLength = match.Length;

}

else

{

// no match

_currentIndex = -1;

}

}

else

{

// not a regular expression search, just match the literal string

_currentIndex = txtControl.Text.IndexOf(searchString, _currentIndex);

}

}

else

{

// this section is for case-insensitive searches

if
(useRegularExpression)

{

try

{

// set the ignore case option and Multiline option for regular expressions

regularExpression = new Regex(searchString,

RegexOptions.IgnoreCase | RegexOptions.Multiline);

}

catch
(Exception ex)

{

MessageBox.Show("Invalid Regular expression");

return
false
;

}

// Now match the regular expression

Match match = regularExpression.Match(txtControl.Text, _currentIndex);

// if we successfully matched, get the index location of the match inside

// the textbox control and the length of the match

if
(match.Success)

{

_currentIndex = match.Index;

searchLength = match.Length;

}

else

{

// no match

_currentIndex = -1;

}

}

else

{

// this search is for non-regular expressions case-insensitive

CultureInfo culture = new
CultureInfo("en-us");

_currentIndex = culture.CompareInfo.IndexOf(txtControl.Text, searchString,

_currentIndex, System.Globalization.CompareOptions.IgnoreCase);

}

}

// if we found a match, select it in the multiline text box

if
(_currentIndex >= 0)

{

// (note: this should be refactored, but is shown in one place for the sake of the

// article.)

// select the matching text

txtControl.SelectionStart = txtControl.Text.IndexOf("/n", _currentIndex) + 2;

txtControl.SelectionLength = 0;

txtControl.SelectionStart = _currentIndex;

txtControl.SelectionLength = searchLength;

_currentIndex += searchLength;
// advance past selection

txtControl.ScrollToCaret();
// scroll to selection

}

else

{

// no match, reached the end of the document

MessageBox.Show("Reached the end of the document.");

_currentIndex = 0;

return
false
;

}

return
true
;

}

will this help you...

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐