2020-12-31

C#实现简单的记事本
首先
1.放置一个MenuStrip,第一行为文件、格式、样式、打开信息。文件里边是打开和保存。格式里边是自动换行。样式里边是字体、字体颜色。打开信息里边可以查看打开文档的文件名(打开信息中包括显示和隐藏)。
2.在放置一个TextBox控件,铺满全屏。
3.在TextBox的左侧,放置一个Panel容器,用来放置一个ListBox列表控件和一个Button按钮。
以上是设计器的设计。

        private void Form1_Load(object sender, EventArgs e)//窗体加载时隐藏容器并且将取消自动换行
        {
            panel1.Visible = false;
            textBox1.WordWrap = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;//点击按钮隐藏容器
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;点击显示容器
        }

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        List<string> list = new List<string>();//定义一个泛型集合
        //打开文件功能的实现
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要打开的文件呦亲";
            ofd.InitialDirectory = @"C:\Users\Guo\Desktop";
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            ofd.Multiselect = true;

            ofd.ShowDialog();
            string path = ofd.FileName;
            list.Add(path);
            string fileName = Path.GetFileName(path);
            listBox1.Items.Add(fileName);
            //如果在打开文件对话框中不选择如何文件直接取消,将会报错,因为路径不能为空
            if (path=="")
            {
                return;//直接返回到这个方法
            }
            //使用文件流来将打开文件对话框中的文本文档读取到textbox中(我的理解:文件流可以理解为一条河流,先将打开文件的路径放到这条河流中,河流往下流动,这时需要将路径中的信息传到textbox中,但是不能直接传送,需要先将其放到字节中,在将字节编码到textbox中。保存文件与之正好相反)
            using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                int r=fsRead.Read(buffer, 0, buffer.Length);
                textBox1.Text=Encoding.Default.GetString(buffer, 0, r);
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "请选择要保存的位置";
            sfd.Filter = "文本文件|*.txt|所有文件|*.*";
            sfd.InitialDirectory = @"C:\Users\Guo\Desktop";
            sfd.ShowDialog();

            string path = sfd.FileName;
            if(path=="")
            {
                return;
            }
            using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buffer=Encoding.Default.GetBytes(textBox1.Text);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自动换行ToolStripMenuItem.Text == "自动换行") 
            {
                textBox1.WordWrap = true;
                自动换行ToolStripMenuItem.Text = "取消自动换行";
            }
            else if(自动换行ToolStripMenuItem.Text=="取消自动换行")
            {
                textBox1.WordWrap = false;
                自动换行ToolStripMenuItem.Text = "自动换行";
            }
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.ShowDialog();
            textBox1.Font = fd.Font;

        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            textBox1.ForeColor = cd.Color;
        }

        //双击listbox中的文件名可以在textbox显示出来
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            //上边声明泛型集合的作用就是在打开文件的同时,将文件路径同时也送到泛型集合中,因为方法之间的路径不能调用。
            string path = list[listBox1.SelectedIndex];
            string test = list[0];
            MessageBox.Show(test);
            using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                int r=fsRead.Read(buffer, 0, buffer.Length);
                textBox1.Text=Encoding.Default.GetString(buffer, 0, r);
            }
            panel1.Visible = false;
        }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值