找传奇、传世资源到传世资源站!

C# 文档编辑器

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 文档编辑器 C#语言基础-第1张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(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复