資源簡介
一、 實驗要求
(1) 參考系統的寫字板功能,編寫一個小型的文字編輯工具;
(2) 該文檔編輯器,基本功能;
文件操作::新建,打開,保存,退出; //支持rtf文件
編輯操作::復制,剪切,粘貼,全選;
查找與替換: 設計查找(替換)窗口,支持查找(替換)操作 。
格式操作::字體,顏色等;
(3) 附加功能:
MDI多文檔使用方法
二、 設計思路
1. 使用TabControl控件模擬多文檔的編輯效果。TabControl控件的頁(TabPage)是一個容器,通過自定義的MyTabPage可以在該“頁”中包含文檔編輯器控件RichTextBox來編輯文件。
2. “新建文檔”功能:在TabContol控件的使用過程中,添加一頁就是新建MyTabPage并添加到該控件中。
3. “打開文檔”功能:使用richTextBox.LoadFile();方法打開文件。
4. “保存當前頁”功能:使用richTextBox1.SaveFile();方法保存文件。
5. “關閉當前頁”功能:通過RichTextBox的Modified屬性判斷文件內容是否已發生改變。如果文件內容有修改,則提示用戶是否保存,如回答Yes, 保存后關閉,否則直接關閉;如果文件內容沒有修改,則直接關閉;
6. “查找/替換”功能:使用richTextBox.Find();方法實現。
三、 關鍵代碼
新建:
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
MyTabPage newPage = new MyTabPage();
pagenum = 1;
newPage.Text = "tabPage" pagenum.ToString();
newPage.Parent = tclMain;
tclMain.SelectedTab = newPage; //當前頁
newPage.editor.ContextMenuStrip = contextMenuStrip1; //設置右鍵菜單
}
打開:
private void 打開ToolStripMenuItem_Click(object sender, EventArgs e)
{
MyTabPage newPage = new MyTabPage();
newPage.Parent = tclMain;
tclMain.SelectedTab = newPage; //當前頁
newPage.editor.ContextMenuStrip = contextMenuStrip1; //設置右鍵菜單
dlgOpen.Filter = "文本文件(*.txt)|*.txt|富格式文件(*.rtf)|*.rtf|所有文件(*.*)|*.*"; // 指定文件類型選擇項
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
newPage.filename = dlgOpen.FileName; //連著路徑一起存放
//newPage.filename = openFileDialog.FileName.Substring(openFileDialog.FileName.LastIndexOf("\\") 1);
//只存文件名
if (Path.GetExtension(newPage.filename) == ".rtf")
newPage.editor.LoadFile(newPage.filename, RichTextBoxStreamType.RichText);
else
newPage.editor.LoadFile(newPage.filename, RichTextBoxStreamType.PlainText);
newPage.Text = dlgOpen.FileName.Substring(dlgOpen.FileName.LastIndexOf("\\") 1); //顯示文件名
newPage.editor.Modified = false;
}
}
保存:
private void save(MyTabPage tp)
{
if (tp == tclMain.SelectedTab)
{
// 如果文件名為默認名,表示是新文件
if (tp.filename == "NoName")
{
saveAs(tp); //另存為
}
// 否則表示是舊文件
else
{
if (Path.GetExtension(tp.filename) == ".rtf")
tp.editor.SaveFile(tp.filename, RichTextBoxStreamType.RichText);
else
tp.editor.SaveFile(tp.filename, RichTextBoxStreamType.PlainText);
tp.editor.Modified = false;
}
}
}
private void saveAs(MyTabPage tp)
{
SaveFileDialog dlgSave = new SaveFileDialog(); // 動態創建一個文件保存對話框
dlgSave.Title = "保存文本編輯器文件"; // 指定默認的對話框標題
dlgSave.Filter = "文本文件(*.txt)|*.txt|富格式文件(*.rtf)|*.rtf|所有文件(*.*)|*.*";
// 顯示保存文件對話框,且用戶按下了“確認”按鈕
if (dlgSave.ShowDialog() == DialogResult.OK)
{
tp.filename = dlgSave.FileName;
if (Path.GetExtension(tp.filename) == ".rtf")
tp.editor.SaveFile(tp.filename, RichTextBoxStreamType.RichText);
else
tp.editor.SaveFile(tp.filename, RichTextBoxStreamType.PlainText);
tp.Text = tp.filename.Substring(tp.filename.LastIndexOf("\\") 1); //顯示文件名
tp.editor.Modified = false;
}
}
查找:
public void find(string strFind)
{
foreach (MyTabPage tp in tclMain.TabPages) //找到當前頁
{
if (tp == tclMain.SelectedTab)
{
if (findPostion >= tp.editor.Text.Length)
{
MessageBox.Show("已到文本底部,再次查找將從文本開始處查找", "提示", MessageBoxButtons.OK);
findPostion = 0;
return;
}
findPostion = tp.editor.Find(strFind, findPostion, RichTextBoxFinds.MatchCase);
if(findPostion == -1) //沒找到
{
MessageBox.Show("已到文本底部,再次查找將從文本開始處查找", "提示", MessageBoxButtons.OK);
findPostion = 0;
}
else //已經找到
{
tp.editor.Focus();
findPostion = strFind.Length; //下次查找的開始位置在此次找到的字符串后
}
}
}
}
替換:
public void replace(string strReplace)
{
foreach (MyTabPage tp in tclMain.TabPages) //找到當前頁
{
if (tp == tclMain.SelectedTab)
{
if (tp.editor.SelectedText.Length != 0) //如果選取了字符串
tp.editor.SelectedText = strReplace; //替換被選字符串
}
}
}
四、 運行效果圖
新建:
保存:
打開:
關閉:
查找:
替換:
格式:
顏色:
字體:
居中:
全選、復制、粘貼:
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
using?System.IO;????//Path
namespace?ex3
{
????public?partial?class?Editor?:?Form
????{
????????public?Editor()
????????{
????????????InitializeComponent();
????????}
????????private?int?pagenum;
????????int?findPostion?=?0;????//記錄查找位置
????????private?void?新建ToolStripMenuItem_Click(object?sender?EventArgs?e)
????????{
????????????MyTabPage?newPage?=?new?MyTabPage();
????????????pagenum?+=?1;
????????????newPage.Text?=?“tabPage“?+?pagenum.ToString();
????????????newPage.Parent?=?tclMain;
????????????tclMain.SelectedTab?=?newPage;?//當前頁
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????95744??2016-12-13?12:24??Editor\.vs\editor\v14\.suo
?????文件????????982??2016-12-10?12:16??Editor\editor.sln
?????文件????????189??2016-11-10?10:49??Editor\ex3\App.config
?????文件????????140??2016-11-10?16:58??Editor\ex3\bin\Debug\abc.txt
?????文件??????99840??2016-12-11?21:37??Editor\ex3\bin\Debug\ex3.exe
?????文件????????189??2016-11-10?10:49??Editor\ex3\bin\Debug\ex3.exe.config
?????文件??????44544??2016-12-11?21:37??Editor\ex3\bin\Debug\ex3.pdb
?????文件??????22696??2016-12-13?12:16??Editor\ex3\bin\Debug\ex3.vshost.exe
?????文件????????189??2016-11-10?10:49??Editor\ex3\bin\Debug\ex3.vshost.exe.config
?????文件????????490??2016-07-16?19:44??Editor\ex3\bin\Debug\ex3.vshost.exe.manifest
?????文件????????158??2016-12-10?16:52??Editor\ex3\bin\Debug\NoName
?????文件??????13496??2016-12-11?21:37??Editor\ex3\Editor.cs
?????文件???????4353??2016-12-10?20:25??Editor\ex3\editor.csproj
?????文件??????40929??2016-12-10?20:44??Editor\ex3\Editor.Designer.cs
?????文件?????110238??2016-12-10?20:44??Editor\ex3\Editor.resx
?????文件???????1175??2016-12-10?20:44??Editor\ex3\formFindReplace.cs
?????文件???????4972??2016-12-10?20:36??Editor\ex3\formFindReplace.Designer.cs
?????文件???????5817??2016-12-10?20:36??Editor\ex3\formFindReplace.resx
?????文件???????4074??2016-12-11?21:37??Editor\ex3\MyTabPage.cs
?????文件???????1464??2016-11-10?17:11??Editor\ex3\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7264??2016-12-13?12:09??Editor\ex3\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????1869??2016-12-13?12:17??Editor\ex3\obj\Debug\editor.csproj.FileListAbsolute.txt
?????文件???????1139??2016-12-10?20:44??Editor\ex3\obj\Debug\editor.csproj.GenerateResource.Cache
?????文件???????2384??2016-12-10?20:25??Editor\ex3\obj\Debug\editor.csprojResolveAssemblyReference.cache
?????文件????????850??2016-12-10?10:21??Editor\ex3\obj\Debug\ex3.csproj.FileListAbsolute.txt
?????文件???????1012??2016-11-10?17:28??Editor\ex3\obj\Debug\ex3.csproj.GenerateResource.Cache
?????文件???????2384??2016-11-10?12:22??Editor\ex3\obj\Debug\ex3.csprojResolveAssemblyReference.cache
?????文件??????69939??2016-12-10?20:44??Editor\ex3\obj\Debug\ex3.Editor.resources
?????文件??????99840??2016-12-11?21:37??Editor\ex3\obj\Debug\ex3.exe
?????文件????????180??2016-12-10?20:36??Editor\ex3\obj\Debug\ex3.FormFindReplace.resources
............此處省略27個文件信息
評論
共有 條評論