資源描述:
《在文件中查找替換(c#代碼)》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_179{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}publicintstart;privatevoidbutto
2、n1_Click(objectsender,EventArgse){try{richTextBox1.LoadFile(textBox1.Text,RichTextBoxStreamType.RichText);}catch(Exceptione1){MessageBox.Show("文?件t名?錯洙?誤ó.","提?示?對?話°框ò");}richTextBox1.Focus();}privatevoidForm1_Load(objectsender,EventArgse){label1.Text="filename";button1.Text="
3、open";button2.Text="save";label2.Text="findtext";label3.Text="replace";button3.Text="findnext";button4.Text="replaceall";}privatevoidbutton2_Click(objectsender,EventArgse){if(richTextBox1.Modified){richTextBox1.SaveFile(textBox1.Text,RichTextBoxStreamType.RichText);//打洙?開a寫′字?板?
4、文?件t//richTextBox1.SaveFile(textBox1.Text,RichTextBoxStreamType.PlainText);//打洙?開atxt文?本?文?檔苔?richTextBox1.Modified=false;MessageBox.Show("已?經(jīng)-保饋?存?");richTextBox1.Focus();}}privatevoidtextBox2_TextChanged(objectsender,EventArgse){start=0;}privatevoidbutton3_Click(objectsender,E
5、ventArgse){stringstr1;str1=textBox2.Text;start=richTextBox1.Find(str1,start,RichTextBoxFinds.MatchCase);if(start==-1){MessageBox.Show("已?經(jīng)-到?結(jié)á尾2","查é找ò結(jié)á束?對?話°框ò");start=0;}elsestart=start+str1.Length;richTextBox1.Focus();}privatevoidbutton4_Click(objectsender,EventArgse){strin
6、gstr1,str2;str1=textBox2.Text;str2=textBox3.Text;start=richTextBox1.Find(str1,start,RichTextBoxFinds.MatchCase);while(start!=-1){richTextBox1.SelectedText=str2;start=start+str2.Length;start=richTextBox1.Find(str1,start,RichTextBoxFinds.MatchCase);}MessageBox.Show("已?全?部?替?換?","查
7、é找ò結(jié)á束?對?話°框ò");start=0;richTextBox1.Focus();}/*privatevoidopenFileDialog1_FileOk(objectsender,CancelEventArgse){OpenFileDialogofd=newOpenFileDialog();ofd.InitialDirectory="d:\";ofd.Filter="*.rtf";}*/}}