您的位置:首页 > 编程语言 > C#

c#简单实现记事本功能

2015-10-23 22:32 381 查看
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 =
4000
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);

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: