c#简单实现记事本功能

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

namespace 进程和线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Visible = false;       //隐藏listbox,即历史记录
        }
        String[] path;
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文本文件";
            ofd.Multiselect = true;
            ofd.Filter = "文本文件|*.txt";
            ofd.ShowDialog();
            path = ofd.FileNames;
            if (path.Length <= 0)
            {
                return;
            }
            for (int i = 0; i < path.Length; i++)
            {
                listBox1.Items.Add(Path.GetFileName(path[i]));
            }
            ShowContent(0);
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "请选择保存的文件夹";
            sfd.InitialDirectory = @"c:\";
            sfd.Filter = "文本文件|*.txt";
            sfd.ShowDialog();
            String fileName = sfd.FileName;
            if(fileName == "")
            {
                return;
            }
            using (FileStream fwrite = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                byte[] buf = new byte[1024 * 1024 * 5];
                buf = Encoding.UTF8.GetBytes(textBox1.Text);
                fwrite.Write(buf, 0, buf.Length);
            }
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!textBox1.WordWrap)
            {
                textBox1.WordWrap = true;
                自动换行ToolStripMenuItem.Text = "取消自动换行";
            }
            else
            {
                自动换行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;
        }

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

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

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

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            ShowContent(listBox1.SelectedIndex);
        }

        private void ShowContent(int p)
        {
            using (FileStream fread = new FileStream(path[p], FileMode.Open))
            {
                byte[] buf = new byte[1024 * 1024 * 5];
                fread.Read(buf, 0, buf.Length);
                textBox1.Text = System.Text.Encoding.UTF8.GetString(buf);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值