winform 记事本

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;//流的命名空间
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //--------------------------------------------------编辑
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.CanUndo)//撤销文本内容后 实现不可再撤销
            {
                撤销ToolStripMenuItem.Enabled = true;
            }
        }
        private string bianhua;//成员变量 如果是保存的文本选中 不是撤销
        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.CanUndo==true&&bianhua==null)//判断缓存区是否有东西 有东西允许撤销true
            {
                textBox1.Undo();//撤销
                textBox1.ClearUndo();//清空撤销缓存区
                撤销ToolStripMenuItem.Enabled = false;
            }
            else
            {
                textBox1.Select(chushi.Length, bianhua.Length);
            }
        }
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Cut();//剪切
        }
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Copy();//复制
        }
        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Paste();//粘贴
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectedText = "";//删除
        }
        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(this);
            //f.Focus();
            f.Show();
          
        }
        private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f = new Form3(this);
            //f.Focus();
            f.Show();
        }
        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();//全选
        }

       



        //------------------------------------------文件
        private string FileName;
        private string chushi;
       
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)//新建
        {
            if (this.textBox1.Text.Length > 0)
            {
                DialogResult drg = MessageBox.Show("是否进行保存?", "保存对话框", MessageBoxButtons.YesNo);
                if (DialogResult.Yes == drg)
                {
                    if (FileName == null)
                    {
                        DialogResult dr = saveFileDialog1.ShowDialog();
                        if (dr == DialogResult.OK)
                        {
                            string filename = saveFileDialog1.FileName;
                            //写入流,可以在硬盘上创建文件,并为文件写入信息
                            StreamWriter sw = new StreamWriter(filename);
                            sw.Write(textBox1.Text);
                            sw.Close();
                        }
                    }
                    else
                    {
                        //写入流,可以在硬盘上创建文件,并为文件写入信息
                        StreamWriter sw = new StreamWriter(FileName);
                        sw.Write(this.textBox1.Text);
                        sw.Close();
                    }
                }
            }

            FileName = null;
            this.textBox1.Text = "";
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)//打开
        {
            //OpenFileDialog openFileDialog1 = new OpenFileDialog();//手动代码打开
            DialogResult dr = openFileDialog1.ShowDialog();
            openFileDialog1.Filter = "文本文件|*.txt;*.avi;*.jpg";//打开文件的格式
            if (dr == DialogResult.OK)//判断是否点击打开开按钮
            {
                string filename = openFileDialog1.FileName;//获取文件的路径
                FileName = filename;
                //通过读入流进行文件读取
                StreamReader sr = new StreamReader(filename, Encoding.Default);//流的读取  流只是一个通道
                textBox1.Text = sr.ReadToEnd();//从头读到尾
                sr.Close();//必须要关闭流
                chushi = textBox1.Text;
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)//保存
        {
            saveFileDialog1.Filter = "文本文件|*.txt";
            if (FileName == null)//新建的文本文件  弹出对话框
            {
                DialogResult dr = saveFileDialog1.ShowDialog();
                if (dr == DialogResult.OK)//说明点击保存了
                {
                    string filename = saveFileDialog1.FileName;//获取路径
                    //写入流,可以在硬盘上创建文件,并为文件写入信息
                    StreamWriter sw = new StreamWriter(filename);//流写写入 new建取一个缓存区
                    sw.Write(this.textBox1.Text);
                    sw.Close();
                }
            }
            else
            {
                //写入流,可以在硬盘上创建文件,并为文件写入信息
                StreamWriter sw = new StreamWriter(FileName);
                sw.Write(this.textBox1.Text);
                sw.Close();
                bianhua = this.textBox1.Text.Substring(chushi.Length);
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)//另存为
        {
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|word文件(*.doc)|*.doc";
            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;
                //写入流,可以在硬盘上创建文件,并为文件写入信息
                StreamWriter sw = new StreamWriter(filename);
                sw.Write(this.textBox1.Text);
                sw.Close();
            }
        }

        private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)//页面设置
        {
            pageSetupDialog1.Document = printDocument1;//为页面设置对话框指定打印对象   连接
            pageSetupDialog1.ShowDialog();//打开页面对话框
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)//打印
        {
            DialogResult dr = printDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                printDocument1.Print();//打印的对象
            }
        }
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//事件//e事件数据  就是printDocument1_PrintPage事件
        {
            //设置打印的画板内容
            System.Drawing.Font f = new System.Drawing.Font("宋体", 12);//字体需要font类型的 所以要new新对象  字体对象
            e.Graphics.DrawString(this.textBox1.Text, f, SystemBrushes.ActiveBorder, 10.0f, 0f);//e为每一页的数据 Graphics空白页
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//退出
        {
            this.Close();
        }
        //-----------------------------------------格式
        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)//自动换行
        {
            //textBox1.WordWrap = !textBox1.WordWrap;//点一下换行再点一下不换行
            自动换行ToolStripMenuItem.Checked = textBox1.WordWrap;//选上Checked自动换行
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)//字体
        {
            fontDialog1.ShowDialog();
            MessageBox.Show(fontDialog1.Font.Size.ToString());
        }

      
    }
}

  

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

namespace WindowsFormsApplication3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private Form1 FuForm; //定义form1类的FuForm 来接收主窗体的内容
        public Form2(Form1 jishiben)//通过构造函数来传值 构造函数的重载 括号里是form1的成员变量
        {
            InitializeComponent();//系统自带的初始化对象
            FuForm =(Form1) jishiben;//将主窗体赋值给form2中的form1类jishiben
        }
        private int count;//定义int类型 索引从0开始
        private void button1_Click(object sender, EventArgs e)//查找下一个
        {
            string cztxt = txtchazhao.Text;//定义"查找"窗口的文字赋值给cztxt
            if (count == 0)//索引从0开始 代表第一次查找
            {
                int index = FuForm.textBox1.Text.IndexOf(cztxt,0);
                if (index >= 0)
                {
                    FuForm.textBox1.Select(index, cztxt.Length);//选中查找的内容
                    count = FuForm.textBox1.Text.IndexOf(cztxt) + 1;//索引加1 查询以后
                    FuForm.textBox1.Focus();//光标聚焦到选中文字上
                }
                else
                {
                    MessageBox.Show("您所要查询的内容不存在");
                }
            }
            else
            {
                int index = FuForm.textBox1.Text.IndexOf(cztxt, count);
                if (index >= 0)
                {
                    FuForm.textBox1.Select(index, cztxt.Length);
                    count = FuForm.textBox1.Text.IndexOf(cztxt, count) + 1;
                    FuForm.textBox1.Focus();
                }
                else
                {
                    MessageBox.Show("您所要查询的内容不存在");
                }
            }
        }

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

  

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

namespace WindowsFormsApplication3
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
         private Form1 FuForm; //定义form1类的FuForm 来接收主窗体的内容
        public Form3(Form1 jishiben)//通过构造函数来传值 构造函数的重载 括号里是form1的成员变量
        {
            InitializeComponent();//系统自带的初始化对象
            FuForm =(Form1) jishiben;//将主窗体赋值给form2中的form1类jishiben
        }
        private int count;
        private void button1_Click(object sender, EventArgs e)
        {
            string cztxt = txtchazhao.Text;//定义"查找"窗口的文字赋值给cztxt
            if (count == 0)//索引从0开始 代表第一次查找
            {
                int index = FuForm.textBox1.Text.IndexOf(cztxt, 0);
                if (index >= 0)
                {
                    FuForm.textBox1.Select(index, cztxt.Length);//选中查找的内容
                    count = FuForm.textBox1.Text.IndexOf(cztxt) + 1;//索引加1 查询以后
                    FuForm.textBox1.Focus();//光标聚焦到选中文字上
                }
            }
            else
            {
                int index = FuForm.textBox1.Text.IndexOf(cztxt, count);
                if (index >= 0)
                {
                    FuForm.textBox1.Select(index, cztxt.Length);
                    count = FuForm.textBox1.Text.IndexOf(cztxt, count) + 1;
                    FuForm.textBox1.Focus();
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if ( FuForm.textBox1.SelectedText.Length>0)//去掉不选中自替换的现象
            {
                string ss = textBox2.Text;
            FuForm.textBox1.SelectedText = ss;
            FuForm.textBox1.Focus();
            }
            
        }

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

  

转载于:https://www.cnblogs.com/Mr-xue/p/4607178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值