在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 文档编辑器

C# 文档编辑器

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.18M
  • 下载次数:31
  • 浏览次数:448
  • 发布时间:2015-11-17
  • 实例类别:C#语言基础
  • 发 布 人:寒风雪
  • 文件格式:.zip
  • 所需积分:2
 相关标签:

实例介绍

【实例简介】

【实例截图】

【核心代码】

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# 文档编辑器

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警