基本信息
源码名称:C# 文本编辑器示例源码(仿记事本notepad)
源码大小:0.11M
文件格式:.zip
开发语言:C#
更新时间:2017-12-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//Download by http://www.codefans.net
namespace 多文档编辑器
{
    public partial class MainForm : Form
    {
        private int wCount = 0;
        private RichTextBoxStreamType oldFileType;
        private DocForm doc;

        private static DocForm docSend;
        private string fileName = string.Empty;
        private string filePath = string.Empty;

        List<DocForm> listDocForm = new List<DocForm>();

        string _connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\FileData.mdb", Environment.CurrentDirectory);

        public MainForm()
        {
            InitializeComponent();
        }

        public static DocForm GetDocTrun()
        {
            return docSend;
        }

        private void tsmi_Format_Wrap_Click(object sender, EventArgs e)     //自动换行
        {
            try
            {
                if (tsmi_Format_Wrap.CheckState == CheckState.Checked)
                {
                    tssl_FormCount.Text = string.Format("   成功关闭自动换行功能 ......");
                    tsmi_Format_Wrap.CheckState = CheckState.Unchecked;
                    DocForm df = (DocForm)this.ActiveMdiChild;
                    df.Sourse.WordWrap = false;
                }
                else
                {
                    tssl_FormCount.Text = string.Format("   成功启动自动换行功能 ......");
                    tsmi_Format_Wrap.CheckState = CheckState.Checked;
                    DocForm df = (DocForm)this.ActiveMdiChild;
                    df.Sourse.WordWrap = true;
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

        private void tsmi_Format_Font_Click(object sender, EventArgs e)     //字体大小设置
        {
            tssl_FormCount.Text = string.Format("   启动字体大小设置功能 ......");
            try
            {
                if (fontDialog1.ShowDialog() == DialogResult.OK && doc != null)
                {
                    DocForm df = (DocForm)this.ActiveMdiChild;
                    df.Sourse.SelectionFont = fontDialog1.Font;
                }
            }
            catch (Exception ex)
            {
                return;
            }
            tssl_FormCount.Text = string.Format("   关闭字体大小设置功能 ......");
        }

        private void tsmi_Format_Colour_Click(object sender, EventArgs e)   // 字体颜色设置
        {
            tssl_FormCount.Text = string.Format("   启动字体颜色设置功能 ......");
            try
            {
                if (colorDialog1.ShowDialog() == DialogResult.OK && doc != null)
                {
                    DocForm df = (DocForm)this.ActiveMdiChild;
                    df.Sourse.SelectionColor = colorDialog1.Color;
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }



        private void tsmi_File_NewCreate_Click(object sender, EventArgs e)  //新建文件
        {
            wCount  ;
            doc = new DocForm();
            listDocForm.Add(doc);

            doc.MdiParent = this;
            doc.Text = "文档"   wCount;
            doc.Show();

            if (wCount == 1)
            {
                doc.WindowState = FormWindowState.Maximized;
            }
            tssl_FormCount.Text = string.Format("   成功创建第 {0} 个文件......当前文件名为:  {1} ", this.MdiChildren.Length,doc.Text);
        }

        private void tsmi_File_Open_Click(object sender, EventArgs e)       //打开文件
        {
            tssl_FormCount.Text = string.Format("   正在打开文件 ......");
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    RichTextBoxStreamType fileType = TrunFileType(openFileDialog1.FilterIndex);
                    wCount  ;
                    doc = new DocForm(fileType, openFileDialog1.FileName, openFileDialog1.FilterIndex);
                    doc.MdiParent = this;
                    doc.Show();
                    listDocForm.Add(doc);
                }
            }
            catch (Exception ex)
            {
                return;
            }

            string str = openFileDialog1.FileName;
            string[] sArray = str.Split('\\');
            doc.Text = sArray[sArray.Length - 1];

            tssl_FormCount.Text = string.Format("   成功打开文件,当前文件名为:  {0} ", doc.Text);
            doc.WindowState = FormWindowState.Maximized;
        }

        private void tsmi_File_Save_Click(object sender, EventArgs e)       //保存文件
        {
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                if (df.GetFilePath() == "")
                {
                    tssl_FormCount.Text = string.Format("   请选择保存路径");
                    SaveFile(df);
                    //SaveDateToDateBase(df);
                    MessageBox.Show("保存成功", "温馨提示");
                    tssl_FormCount.Text = string.Format("   {0}  保存成功 ", doc.Text);
                }
                else
                {
                    RichTextBoxStreamType fileType = TrunFileType(df.GetFileTypeIndex());
                    df.Sourse.SaveFile(df.GetFilePath(), fileType);
                    MessageBox.Show("保存成功", "温馨提示");
                    tssl_FormCount.Text = string.Format("   {0}  保存成功 ", doc.Text);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存失败,请重新保存!","温馨提示");
            }
        }

        private void tsmi_File_OtherSave_Click(object sender, EventArgs e)  //另存为
        {
            tssl_FormCount.Text = string.Format("   请选择保存路径");
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                SaveFile(df);
                filePath = df.GetFilePath();
                //SaveDateToDateBase(df);
                MessageBox.Show("保存成功","温馨提示");
            }
            catch (Exception ex)
            {
                return;
            }
            tssl_FormCount.Text = string.Format("   {0}  保存成功 ", doc.Text);
        }

        private void tsmi_File_SaveToDateBase_Click(object sender, EventArgs e)//保存到数据库
        {
            try
            {
                DocForm df = (DocForm)this.ActiveMdiChild;
                fileName = df.Text;
                if (filePath == "")
                {
                    filePath = Environment.CommandLine;
                }
                SaveDateToDateBase(df);
            }
            catch (Exception ex)
            {
                return;
            }
        }

        private void tsmi_File_PrintPage_Click(object sender, EventArgs e)  //打印设置
        {
            tssl_FormCount.Text = string.Format("   进行打印设置...... ");
            PrintDialog pd = new PrintDialog();
            pd.ShowDialog();
        }

        private void tsmi_File_PageSet_Click(object sender, EventArgs e)    //页面设置
        {
            tssl_FormCount.Text = string.Format("   进行页面设置...... ");
            pageSetupDialog1.Document = new PrintDocument();

            this.pageSetupDialog1.AllowMargins = true;
            this.pageSetupDialog1.AllowOrientation = true;
            this.pageSetupDialog1.AllowPaper = true;
            this.pageSetupDialog1.AllowPrinter = true;

            this.pageSetupDialog1.ShowDialog();
        }

        private void tsmi_File_Exit_Click(object sender, EventArgs e)       //退出程序
        {
            Application.Exit();
        }



        private void tsmi_Edit_Copy_Click(object sender, EventArgs e)   //复制
        {
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                if (df.Sourse.SelectionLength > 0)
                {
                    df.Sourse.Copy();
                }
            }
            catch
            {
                return;
            }
            tssl_FormCount.Text = string.Format("   复制成功   ");
        }

        private void tsmi_Edit_Stick_Click(object sender, EventArgs e)  //粘贴
        {
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                df.Sourse.Paste();
                tssl_FormCount.Text = string.Format("   粘贴成功   ");
            }
            catch (Exception ex)
            {
                return;
            }
        }

        private void tsmi_Edit_Cut_Click(object sender, EventArgs e)    //剪接
        {
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                if (df.Sourse.SelectionLength > 0)
                {
                    df.Sourse.Cut();
                }
            }
            catch (Exception ex)
            {
                return;
            }
            tssl_FormCount.Text = string.Format("   剪接成功   ");
        }

        private void tsmi_Edit_Delete_Click(object sender, EventArgs e) //删除
        {
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                if (df.Sourse.SelectionLength > 0)
                {
                    df.Sourse.SelectedText = "";
                }
            }
            catch(Exception ex)
            {
                return;
            }
            tssl_FormCount.Text = string.Format("   删除成功   ");
        }

        private void tsmi_Edit_Cancel_Click(object sender, EventArgs e) //撤销
        {
            DocForm df = (DocForm)this.ActiveMdiChild;
            try
            {
                if (df.Sourse.CanUndo == true)
                {
                    df.Sourse.Undo();
                }
            }
            catch(Exception ex)
            {
                return;
            }
            tssl_FormCount.Text = string.Format("   撤销成功   ");
        }

        private void tsmi_Edit_Find_Click(object sender, EventArgs e)   //查找
        {
            docSend = (DocForm)this.ActiveMdiChild;
            FindForm find = new FindForm();
            find.Show();
            tssl_FormCount.Text = string.Format("   正在进行查找或替换   ");
        }



        private void tsmi_Edit_AllSelect_Click(object sender, EventArgs e)  //全选
        {
            try
            {
                DocForm df = (DocForm)this.ActiveMdiChild;
                df.Sourse.SelectAll();
                tssl_FormCount.Text = string.Format("   已全选......   ");
            }
            catch (Exception ex)
            {
                return;
            }
        }

        private void tsmi_Edit_Date_Click(object sender, EventArgs e)   //日期时间
        {
            try
            {
                DocForm df = (DocForm)this.ActiveMdiChild;
                Clipboard.SetText(DateTime.Now.ToString());
                df.Sourse.Paste();
                tssl_FormCount.Text = string.Format("   输出日期和时间   ");
            }
            catch (Exception ex)
            {
                return;
            }
        }



        private void tsmi_Help_LookForHelp_Click(object sender, EventArgs e)    //帮助信息
        {
            tssl_FormCount.Text = string.Format("    打开帮助信息......");
            MessageBox.Show("感谢你的使用,详情请联系开发者!", "温馨提示");
        }

        private void tsmi_Help_AboutTheSoft_Click(object sender, EventArgs e)   //关于软件信息
        {
            tssl_FormCount.Text = string.Format("    查询软件信息......");
            MessageBox.Show("软件版本:1.0正式版\n\r开发者:伍伟劲\n\r联系方式:fish_yuxi@hotmail.com", "温馨提示");
        }

        private void tsmi_Check_Statue_Click(object sender, EventArgs e)        //状态栏
        {
            try
            {
                if (tsmi_Check_Statue.CheckState == CheckState.Checked)
                {

                    tsmi_Check_Statue.CheckState = CheckState.Unchecked;
                    stasp_StatueBar.Visible = false;
                }
                else
                {
                    tsmi_Check_Statue.CheckState = CheckState.Checked;
                    stasp_StatueBar.Visible = true;
                    tssl_FormCount.Text = string.Format("   启动状态栏功能");
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }




        public void ExecuteSql(string sql)                  //执行sql语句
        {
            using (OleDbConnection conn = new OleDbConnection(_connectionString))
            {
                using (OleDbCommand command = new OleDbCommand())
                {
                    try
                    {
                        conn.Open();
                        command.Connection = conn;
                        command.CommandType = System.Data.CommandType.Text;
                        command.CommandText = sql;
                        command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        conn.Close();
                    }
                }
            }
        }

        public DataTable ReadDataToDataTable(string sql)    //输出数据库数据
        {
            using (OleDbConnection conn = new OleDbConnection(_connectionString))
            {
                using (OleDbCommand command = new OleDbCommand())
                {

                    conn.Open();
                    command.Connection = conn;
                    command.CommandType = System.Data.CommandType.Text;

                    DataTable dt = new DataTable();
                    OleDbDataAdapter adapter = new OleDbDataAdapter();

                    command.CommandText = sql;
                    adapter.SelectCommand = command;
                    adapter.Fill(dt);
                    return dt;
                }
            }
        }

        public void SaveFile(DocForm df)                    //保存文件
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                RichTextBoxStreamType fileType = TrunFileType(sfd.FilterIndex);
                doc.SetFileTypeIndex(sfd.FilterIndex);
                doc.SetFilePath(saveFileDialog1.InitialDirectory);
                df.Sourse.SaveFile(sfd.FileName, fileType);

                df.SetFilePath(sfd.FileName);
                oldFileType = fileType;
            }
            string str = df.GetFilePath();

            string[] sArray = str.Split('\\');
            fileName = sArray[sArray.Length - 1];

            for (int i = 0; i < sArray.Length - 1; i  )
            {
                filePath  = sArray[i];
            }
        }

        public void SaveDateToDateBase(DocForm df)          //保存到数据库
        {
            try
            {
                DataTable dtPath = new DataTable();
                dtPath = ReadDataToDataTable("select fNumber,fName from tFilePath");

                DataTable dtContent = new DataTable();
                dtContent = ReadDataToDataTable("select fNumber,fName from tFileContent");

                if (dtPath.Rows.Count != 0)
                {
                    for (int i = 0; i < dtPath.Rows.Count; i  )
                    {
                        if (fileName == (Convert.ToString(dtPath.Rows[i]["fName"])))
                        {
                            int idPath = Convert.ToInt32(dtPath.Rows[i]["fNumber"]);
                            ExecuteSql(string.Format("update tFilePath set fPath='{0}', where fNumber='{1}'", filePath, idPath));
                        }
                        else
                            ExecuteSql(string.Format("insert into tFilePath(fName,fPath) values('{0}','{1}')", fileName, filePath));
                    }
                }
                else
                    ExecuteSql(string.Format("insert into tFilePath(fName,fPath) values('{0}','{1}')", fileName, filePath));


                if (dtContent.Rows.Count != 0)
                {
                    for (int i = 0; i < dtContent.Rows.Count; i  )
                    {
                        if (fileName == (Convert.ToString(dtContent.Rows[i]["fName"])))
                        {
                            int idContent = Convert.ToInt32(dtContent.Rows[i]["fNumber"]);
                            ExecuteSql(string.Format("update tFileContent set fContent='{0}',fDate='{1}', where fNumber='{2}'", df.Sourse.Text, DateTime.Now, idContent));
                        }
                        else
                            ExecuteSql(string.Format("insert into tFileContent(fName,fContent,fDate) values('{0}','{1}','{2}')", fileName, df.Sourse.Text, DateTime.Now));

                    }
                }
                else
                    ExecuteSql(string.Format("insert into tFileContent(fName,fContent,fDate) values('{0}','{1}','{2}')", fileName, df.Sourse.Text, DateTime.Now));
                MessageBox.Show("保存到数据库成功!", "温馨提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存到数据库失败失败,请重新保存!","温馨提示");
            }
        }

        public RichTextBoxStreamType TrunFileType(int i)   //数据转换
        {
            RichTextBoxStreamType fileType;
            switch (i)
            {
                case 1: fileType = RichTextBoxStreamType.PlainText;
                    break;
                case 2: fileType = RichTextBoxStreamType.RichText;
                    break;
                default: fileType = RichTextBoxStreamType.UnicodePlainText;
                    break;
            }
            return fileType;
        }

        private void tsmi_File_History_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

    }
}