Noteopad – Searching and replacing

private void ButtonFindNext_Click(object sender,EventArgs eArgs)
{
int Pos=-1;
if(IsDirectionDownward)
{
if(TextArea.SelectionLength==0)
Pos=TextArea.Text.IndexOf(TextToFind,TextArea.SelectionStart);
else
Pos=TextArea.Text.IndexOf(TextToFind,TextArea.SelectionStart+TextArea.SelectionLength);
}
else
{
if(TextArea.SelectionStart>0)
Pos=TextArea.Text.LastIndexOf(TextToFind,TextArea.SelectionStart-1);
}
if(Pos!=-1)
{
TextArea.SelectionStart=Pos;
TextArea.SelectionLength=TextToFind.Length;
if(sender!=null)
((Control)sender).Focus();
}
else
{
MessageBox.Show(“Cannot Find: \”” + TextToFind + “\””);
}
}

private string ReplaceString(string StrSource,string StrFind,string StrReplace)
{
int iPos=StrSource.IndexOf(StrFind);
String StrReturn=””;
while(iPos!=-1)
{
StrReturn+=StrSource.Substring(0,iPos)+StrReplace;
StrSource=StrSource.Substring(iPos+StrFind.Length);
iPos=StrSource.IndexOf(StrFind);
}
if(StrSource.Length>0)
StrReturn+=StrSource;
return StrReturn;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s