基本信息
源码名称:c# 小型的文字编辑工具源码
源码大小:0.34M
文件格式:.rar
开发语言:C#
更新时间:2017-06-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍


一、     实验要求

(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;   //替换被选字符串

                }

            }

        }

 

四、     运行效果图

新建:

 

保存:

打开:

 

关闭:

 

查找:

 

 

 

替换:

 

格式:

颜色:

字体:

居中:

 

全选、复制、粘贴: