Friday, July 01, 2005

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;
}

0 Comments:

Post a Comment

<< Home