开发者社区> JKXQJ> 正文

C#记事本

简介: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows
+关注继续查看

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TXT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified)
            {
                /*提示保存对话框*/
                DialogResult dResult = MessageBox.Show("文件" + this.Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dResult)
                {
                    case DialogResult.Yes:
                        另存为ToolStripMenuItem_Click(null, null);
                        richTextBox1.Clear();
                        this.Text = "无标题-记事本";
                        break;
                    case DialogResult.No:
                        richTextBox1.Clear();
                        this.Text = "无标题-记事本";
                        break;
                    case DialogResult.Cancel:
                        break;
                }
            }
            else
            {
                richTextBox1.Clear();
                this.Text = "无标题-记事本";
                richTextBox1.Modified = false;
            }
        }

        private static string openfilepath = "";			//保存所打开文件的路径

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; 	//设置文件类型   
            openFileDialog1.FilterIndex = 1;            	//设置默认文件类型的显示顺序
            openFileDialog1.RestoreDirectory = true;    	//打开对话框是否记忆上次打开的目录
            StreamReader sr = null;					//定义StreamReader对象
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    openfilepath = openFileDialog1.FileName;				//获取打开的文件路径
                    string name = openfilepath.Substring(openfilepath.LastIndexOf("\\") + 1);
                    this.Text = name;								//文件名作为标题
                    sr = new StreamReader(openfilepath, Encoding.Default);	//实例化sr
                    richTextBox1.Text = sr.ReadToEnd();					//读取所有文件内容
                }
                catch
                {
                    MessageBox.Show("打开文件时出错。", "错误",
                       System.Windows.Forms.MessageBoxButtons.OK,
                       System.Windows.Forms.MessageBoxIcon.Warning);
                    return;
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();				//关闭对象sr
                        sr.Dispose();			//释放对象sr资源
                    }
                }
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型   
            saveFileDialog1.FilterIndex = 2;		//设置默认文件类型的显示顺序
            saveFileDialog1.RestoreDirectory = true;	//保存对话框是否记忆上次打开的目录
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                openfilepath = saveFileDialog1.FileName.ToString(); //获取文件路径   
                FileStream fs;
                try
                { fs = File.Create(openfilepath); }
                catch
                {
                    MessageBox.Show("建立文件时出错。", "错误",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                    return;
                }
                byte[] content = Encoding.Default.GetBytes(richTextBox1.Text);
                try
                {
                    fs.Write(content, 0, content.Length);
                    fs.Flush();
                    toolStripStatusLabel1.Text = "保存成功";
                }
                catch
                {
                    MessageBox.Show("写入文件时出错。", "错误",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                }
                finally
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter sw = null;
            if (openfilepath == "")
            {
                另存为ToolStripMenuItem_Click(null, null);      //调用另存为方法
                return;
            }
            try
            {
                sw = new StreamWriter(openfilepath, false, Encoding.Default);
                sw.Write(richTextBox1.Text);
                toolStripStatusLabel1.Text = "保存成功";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", System.Windows.Forms.MessageBoxButtons.
                    OK, System.Windows.Forms.MessageBoxIcon.Warning);
                return;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();     			//关闭StreamWriter
                    sw.Dispose();   			//释放资源
                }
            }
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.WordWrap == true)
            {
                richTextBox1.WordWrap = false;
                自动换行ToolStripMenuItem.Checked = false;
                richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
            }
            else
            {
                richTextBox1.WordWrap = true;
                自动换行ToolStripMenuItem.Checked = true;
                richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedVertical;
            }
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                Font font = fontDialog1.Font;
                richTextBox1.SelectionFont = font;
            }
        }

        private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (statusStrip1.Visible == true)
            {
                statusStrip1.Visible = false;
                状态栏SToolStripMenuItem.Checked = false;
                richTextBox1.Height += 22;
            }
            else
            {
                statusStrip1.Visible = true;
                状态栏SToolStripMenuItem.Checked = true;
                richTextBox1.Height -= 22;
            }
        }

       private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printDialog1.ShowDialog();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
  
        private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.richtextbox = richTextBox1;
            f2.ShowDialog();
        }

        private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            place();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            place();
        }
        private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
        {
            place();
        }
        private void place()										//计算行数与列数
        {
            string str = this.richTextBox1.Text;
            int m = this.richTextBox1.SelectionStart;
            int Ln = 0;
            int Col = 0;
            for (int i = m - 1; i >= 0; i--)
            {
                if (str[i] == '\n')
                    Ln++;
                if (Ln < 1)
                    Col++;
            }
            Ln = Ln + 1;
            Col = Col + 1;
            toolStripStatusLabel1.Text = "行:" + Ln.ToString() + "," + "列:" + Col.ToString();
        }

        private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.richText = richTextBox1;
            f3.ShowDialog();
        }

        private void 撤销UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

        private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = "";
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectionLength > 0)
            {
                richTextBox1.SelectedText = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.
                    Second.ToString() + " " + DateTime.Now.Year.ToString() + "-" + DateTime.
                    Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            }
            else
            {
                richTextBox1.SelectedText += DateTime.Now.Hour.ToString() + ":" + DateTime.
                    Now.Second.ToString() + " " + DateTime.Now.Year.ToString() + "-" + DateTime.
                    Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            }
        }

        private void 关于记事本GToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.Show();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)   //新建
        {
            if (richTextBox1.Modified)
            {
                /*提示保存对话框*/
                DialogResult dResult = MessageBox.Show("文件" + this.Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dResult)
                {
                    case DialogResult.Yes:
                        另存为ToolStripMenuItem_Click(null, null);
                        richTextBox1.Clear();
                        this.Text = "无标题-记事本";
                        break;
                    case DialogResult.No:
                        richTextBox1.Clear();
                        this.Text = "无标题-记事本";
                        break;
                    case DialogResult.Cancel:
                        break;
                }
            }
            else
            {
                richTextBox1.Clear();
                this.Text = "无标题-记事本";
                richTextBox1.Modified = false;
            }

        }

        private void toolStripButton2_Click(object sender, EventArgs e)       //打开
        {
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; 	//设置文件类型   
            openFileDialog1.FilterIndex = 1;            	//设置默认文件类型的显示顺序
            openFileDialog1.RestoreDirectory = true;    	//打开对话框是否记忆上次打开的目录
            StreamReader sr = null;					//定义StreamReader对象
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    openfilepath = openFileDialog1.FileName;				//获取打开的文件路径
                    string name = openfilepath.Substring(openfilepath.LastIndexOf("\\") + 1);
                    this.Text = name;								//文件名作为标题
                    sr = new StreamReader(openfilepath, Encoding.Default);	//实例化sr
                    richTextBox1.Text = sr.ReadToEnd();					//读取所有文件内容
                }
                catch
                {
                    MessageBox.Show("打开文件时出错。", "错误",
                       System.Windows.Forms.MessageBoxButtons.OK,
                       System.Windows.Forms.MessageBoxIcon.Warning);
                    return;
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();				//关闭对象sr
                        sr.Dispose();			//释放对象sr资源
                    }
                }
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)     //保存
        {
            StreamWriter sw = null;
            if (openfilepath == "")
            {
                另存为ToolStripMenuItem_Click(null, null);      //调用另存为方法
                return;
            }
            try
            {
                sw = new StreamWriter(openfilepath, false, Encoding.Default);
                sw.Write(richTextBox1.Text);
                toolStripStatusLabel1.Text = "保存成功";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", System.Windows.Forms.MessageBoxButtons.
                    OK, System.Windows.Forms.MessageBoxIcon.Warning);
                return;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();     			//关闭StreamWriter
                    sw.Dispose();   			//释放资源
                }
            }

        }

        private void toolStripButton4_Click(object sender, EventArgs e)  //剪切
        {
            richTextBox1.Cut();
        }  

        private void toolStripButton5_Click(object sender, EventArgs e)   //粘贴
        {
            richTextBox1.Paste();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)    //另存为
        {
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型   
            saveFileDialog1.FilterIndex = 2;		//设置默认文件类型的显示顺序
            saveFileDialog1.RestoreDirectory = true;	//保存对话框是否记忆上次打开的目录
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                openfilepath = saveFileDialog1.FileName.ToString(); //获取文件路径   
                FileStream fs;
                try
                { fs = File.Create(openfilepath); }
                catch
                {
                    MessageBox.Show("建立文件时出错。", "错误",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                    return;
                }
                byte[] content = Encoding.Default.GetBytes(richTextBox1.Text);
                try
                {
                    fs.Write(content, 0, content.Length);
                    fs.Flush();
                    toolStripStatusLabel1.Text = "保存成功";
                }
                catch
                {
                    MessageBox.Show("写入文件时出错。", "错误",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                }
                finally
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }

        private void toolStripButton7_Click(object sender, EventArgs e)  //查找
        {
            Form2 f2 = new Form2();
            f2.richtextbox = richTextBox1;
            f2.ShowDialog();
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.richText = richTextBox1;
            f3.ShowDialog();
        }  
    }
}

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;

namespace TXT
{
    public partial class Form2 : Form
    {
        public RichTextBox richtextbox;
        public int start = 0;
        public Form2()
        {	InitializeComponent();	 }

        private void button1_Click(object sender, EventArgs e)
        {
            richtextbox.SelectionColor = Color.Blue;            //显示为蓝色
            string str;
            str = textBox1.Text;
            if (checkBox1.Checked)
            {
                if (radioButton2.Checked)
                { checkUp(str); }
                else
                { checkDown(str); }
            }
            else
            {
                if (radioButton1.Checked)
                { uncheckDown(str); }
                else
                { uncheckUp(str); }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void checkDown(string ss)                    	//区分大小写向下查找
        {
            int c = 0;
            int b = 0;
            try
            {
                c = richtextbox.SelectionStart;
                b = richtextbox.Text.IndexOf(ss, c + ss.Length, StringComparison.CurrentCulture);
                richtextbox.SelectionStart = b;
                richtextbox.SelectionLength = ss.Length;
                richtextbox.SelectionColor = Color.Red;      	//显示为红色
            }
            catch
            {
                MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
                    MessageBoxButtons.OK);
                this.textBox1.SelectionStart = c;
                this.textBox1.SelectionLength = ss.Length;
            }
        }
        public void checkUp(string ss)						//区分大小写向上查找
        {
            int c = 0;
            int b = 0;
            try
            {
                c = richtextbox.SelectionStart;
                b = richtextbox.Text.LastIndexOf(ss, c - ss.Length, StringComparison.InvariantCulture);
                richtextbox.SelectionStart = b;
                richtextbox.SelectionLength = ss.Length;
                richtextbox.SelectionColor = Color.Red;         //显示为红色
            }
            catch
            {
                MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
                    MessageBoxButtons.OK);
                richtextbox.SelectionStart = c;
                richtextbox.SelectionLength = ss.Length;
            }
        }
        public void uncheckDown(string ss) 				//不区分大小写向下查找
        {
            int c = 0;
            int b = 0;
            try
            {
                c = richtextbox.SelectionStart;
                b = richtextbox.Text.IndexOf(ss, c + ss.Length, StringComparison.CurrentCultureIgnoreCase);
                richtextbox.SelectionStart = b;
                richtextbox.SelectionLength = ss.Length;
                richtextbox.SelectionColor = Color.Red;         //显示为红色
            }
            catch
            {
                MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
                    MessageBoxButtons.OK);
                richtextbox.SelectionStart = c;
                richtextbox.SelectionLength = ss.Length;
            }
        }
        public void uncheckUp(string ss)					//不区分大小写向上查找
        {
            int c = 0;
            int b = 0;
            try
            {
                c = richtextbox.SelectionStart;
                b = richtextbox.Text.LastIndexOf(ss, c - ss.Length,StringComparison.InvariantCultureIgnoreCase);
                richtextbox.SelectionStart = b;
                richtextbox.SelectionLength = ss.Length;
                richtextbox.SelectionColor = Color.Red;         //显示为红色
            }
            catch
            {
                MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
                    MessageBoxButtons.OK);
                richtextbox.SelectionStart = c;
                richtextbox.SelectionLength = ss.Length;
            }
        }
       
    }
}

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;

namespace TXT
{
    public partial class Form3 : Form
    {
        public int start = 0;
        public RichTextBox richText;
        public Form3()
        {
            InitializeComponent();
        }
           public Form3(RichTextBox rtb)
        {
            richText = rtb;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            string str1;                                        		//存放要查找的文本
            str1 = textBox1.Text.Trim();
            richText.SelectionColor = Color.Blue;               		//显示为蓝色
            start = richText.Find(str1, start, RichTextBoxFinds.MatchCase); 	//查找下一个
            if (start==1){
                MessageBox.Show("已查找到文档的结尾", "查找结束对话框", MessageBoxButtons.OK);
                start = 0;
            }
            else{
                start = start + str1.Length;
            }
            richText.SelectionColor = Color.Red;        				//显示为红色
            richText.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string str1, str2;
            str1 = textBox1.Text;
            str2 = textBox2.Text;
            richText.SelectionColor = Color.Blue;            			//显示为蓝色
            start = richText.Find(str1, start, RichTextBoxFinds.MatchCase);
            if (start==1)
            {
                MessageBox.Show("已替换到文档的结尾", "替换结束对话框", MessageBoxButtons.OK);
                start = 0;
            }

            else
            {
                start = start + str1.Length;
                richText.SelectedText = str2;
            }
            richText.SelectionColor = Color.Red;        				//显示为红色
            richText.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str1, str2;
            str1 = textBox1.Text;
            str2 = textBox2.Text;
            start = 0;
            start = richText.Find(str1, start, RichTextBoxFinds.MatchCase);
            while (start!=1)
            {
                richText.SelectedText = str2;
                start += str2.Length;
                start = richText.Find(str1, start, RichTextBoxFinds.MatchCase);
            }
            MessageBox.Show("已替换到文档的结尾", "替换结束对话框", MessageBoxButtons.OK);
            start = 0;
            richText.Focus();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();	
        }

    }
}



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
C#将数据写入记事本并且从记事本中读出
代码 using System.IO;//写入StreamWriter sw = new StreamWriter( @"C:\temp123.txt");sw.WriteLine("----------------hello----------------");sw.
712 0
[转]scite文本编辑器的说明
scite,也就是SCIntilla based Text Editor, 基于SCIntilla编辑组件的文本编辑器。我们见到的许多文本编辑器都是基于SCIntilla编辑组件的。
1091 0
EverEdit|文本编辑器
EverEdit|文本编辑器
47 0
039.简单的文本编辑器
039.简单的文本编辑器
16 0
五个最佳的文本编辑器
很多场合下我们会用到纯文本编辑器,Windows自带的记事本功能很简陋,因此我们从网友的投票提名中选取了前五个最佳的文本编辑器(实际上有六个)。这些编辑器实际上主要适合程序员使用,他们的清单如下。   Notepad++ (Windows)   优于Windows记事本的一个文本编辑器,完全免费且开源,对于不同的编程语言可以实现语法高亮,代码折叠以及宏,起可定制性非常强。
1218 0
xheditor文本编辑器的简单实用介绍
一、下载最新的xheditor文件包:http://download.csdn.net/source/3574826 二、在aspx页面同目录下,放置一个upload.
910 0
从editplus转到gvim
原来从百度空间里的文章,从新发到博客上 在linux下编辑文本使用的是vim编辑,我一般是使用editplus的,多少有一些不习惯,在这里记录一下,持续更新中 关于从 editplus 转到gvim 的 第一个:文件的编码: 从utf-8到gb2312命令就是:set fileencoding= 缩写就是:set fenc= 第二个:换行的问题set wrap  这个命令的意
1454 0
windows命令行下打开文本文件编辑
windows命令行下打开文本文件编辑
98 0
+关注
JKXQJ
好好学习,天天向上
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载