using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace text{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected void SaveFile(string fileName) { try { Stream stream = File.OpenRead(fileName); //需引用 using system.io using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(richTextBox1.Text); } } catch (IOException ex) { MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } //private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) //{ // if (this.richTextBox1.Text == "") // return; // saveFileDialog1.DefaultExt = "txt"; // saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; // if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel) // return; // string FileName = this.saveFileDialog1.FileName; // if (saveFileDialog1.ShowDialog() == DialogResult.OK && FileName.Length > 0) // { // // Save the contents of the RichTextBox into the file. // richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); // MessageBox.Show("文件已成功保存"); // } private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.richTextBox1.Text == "") return; saveFileDialog1.DefaultExt = "txt"; saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; //if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel) // return; string FileName = this.saveFileDialog1.FileName; if (saveFileDialog1.ShowDialog() == DialogResult.OK && FileName.Length > 0) { // Save the contents of the RichTextBox into the file. richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); MessageBox.Show("文件已成功保存"); } } private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) { if (richTextBox1.Text != "") { MessageBox.Show("是否需要先保存?"); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我¨°要°a显?示º?的Ì?文?件t类¤¨¤型¨ª(*.exe)|*.exe"; if (sfd.ShowDialog() == DialogResult.OK) { string fileName = sfd.FileName; SaveFile(fileName); //该函数为自定义函数,代码在后 richTextBox1.Text = ""; } } } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } //protected void OpenFile(string fileName) //{ // try // { // Stream stream = File.OpenRead(fileName); // using (StreamReader reader = new StreamReader(stream)) // { // String s; // while ((s = reader.ReadLine()) != null) // richTextBox1.Text = s; // } // } // catch (IOException ex) // { // MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); // } //} protected void OpenFile(string fileName) { try { var gb18030 = Encoding.GetEncoding("GB18030"); StreamReader reader; using (reader = new StreamReader(fileName, gb18030, true)) { String s; while ((s = reader.ReadLine()) != null) { richTextBox1.Text = s; richTextBox1.Text = "\n"; } } } catch (IOException ex) { MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { if (richTextBox1.Text != "") { MessageBox.Show("是否需要先保存内容?"); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我¨°要°a显?示º?的Ì?文?件t类¤¨¤型¨ª(*.exe)|*.exe"; if (sfd.ShowDialog() == DialogResult.OK) { string fileName = sfd.FileName; SaveFile(fileName); //该函数为自定义函数,代码在后 richTextBox1.Text = ""; } } OpenFileDialog of = new OpenFileDialog(); of.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我¨°要°a显?示º?的Ì?文?件t类¤¨¤型¨ª(*.exe)|*.exe"; if (of.ShowDialog() == DialogResult.OK) { string fileName = of.FileName; OpenFile(fileName); StreamReader reader = new StreamReader(fileName, System.Text.Encoding.Default); } } private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*"; sfd.InitialDirectory = "D:\\"; sfd.FilterIndex = 0; if (sfd.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sfd.FileName); sw.Write(richTextBox1.Text); sw.Close(); } else return; } private void richTextBox1_TextChanged(object sender, EventArgs e) { this.richTextBox1.Font = new Font("宋体", 12, FontStyle.Bold); } private void Form1_Load(object sender, EventArgs e) { } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (richTextBox1.Text == "")//写if(richTextBox1.Text==null)时直接执行else下面的语句了。 { } else { DialogResult result; result = MessageBox.Show("是否保存?", "舞文 1.0.0", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*"; sfd.InitialDirectory = "D:\\"; sfd.FilterIndex = 0; if (sfd.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sfd.FileName); sw.Write(richTextBox1.Text); sw.Close(); } else return; } else if (result == DialogResult.No) { //this.Close();//什么也不做最好。 } else if (result == DialogResult.Cancel) { e.Cancel = true;//若要取消窗体的关闭操作,请将传递给事件处理程序的 FormClosingEventArgs 的 Cancel 属性设置为 true。 //*********以上程序写于2007-11-13-20:59********* } } } private void 打印ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) { if (richTextBox1.SelectionLength > 0) // 复制文本到剪贴板 richTextBox1.Copy(); } private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) { // 判断剪贴板中是否有文本 if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true) { // 判断文本框中是否有文本选定了 if (richTextBox1.SelectionLength > 0) { // 询问是否覆盖选定的文本 if (MessageBox.Show("你想覆盖选定的文本吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No) // 移动选定文本的位置,即之前选定文本的起始 选定文本的长度 richTextBox1.SelectionStart = richTextBox1.SelectionStart richTextBox1.SelectionLength; } // 将剪贴板中的文本粘贴至文本框 richTextBox1.Paste(); } } private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e) { //if (richTextBox1.CanUndo == true) //{ // // 撤销最后的操作 // richTextBox1.Undo(); // // 从该文本框的撤消缓冲区中清除关于最近操作的信息。 // richTextBox1.Redo(); //} richTextBox1.Undo(); } private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) { if (richTextBox1.SelectedText != "") // 剪切选定的文本至剪贴板 richTextBox1.Cut(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (richTextBox1.SelectedText.Length > 0) { richTextBox1.SelectedText = ""; } } private void 全选ToolStripMenuItem_Click(object sender, EventArgs e) { richTextBox1.SelectAll(); } }}
下载C# 文档编辑器用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论