简单记事本应用

1 布局介绍

1.1 初始化布局

1.2 各个文件菜单项布局

 

 

 

 1.3 整体布局

 1.3.1 布局介绍

红框:历史打开记录列表——所有曾经打开过的文本文件都会以列表的形式呈现给用户

绿框:点击此按钮会隐藏历史记录列表框

黄框:文本编辑部分

2 功能介绍

2.1 文件菜单功能

2.1.1 点击菜单项目会弹出两个子菜单供使用者使用,点击“打开”子菜单或者“保存”子菜单均会弹出相对应的对话框,供使用者使用。

2.2 格式菜单功能

2.2.1 点击格式菜单弹出“自动换行”子菜单,点击它下列文本自动换行,并且“自动换行”子菜单转变成“取消自动换行”子菜单,这样就可以在“自动换行”和“取消自动换行”中来回切换

2.3 样式菜单功能

2.3.1 对于样式的选中,我们提供了两种,一是改变“字体”,二是改变“颜色”,使用者可以根据需求完成对应的功能

2.4打开记录菜单功能

2.4.1这里提供了对历史打开记录的显示和隐藏,使用者可以查看曾经的打开记录,并可以双击选中的记录打开文本文件。

3.核心代码

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 WindowsFormsApp1
{
    public partial class Form10 : Form
    {
        public Form10()
        {
            InitializeComponent();
        }

        private void Form10_Load(object sender, EventArgs e)
        {
            //加载程序时隐藏panel
            panel1.Visible = false;
            //取消文本框的自动换行
            textBox1.WordWrap = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //点击按钮时也隐藏panel
            panel1.Visible=false;
            textBox1.Focus();

        }

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

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }
        /// <summary>
        /// 打开对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
       List<string> list = new List<string>();
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要打开的文本文件";
            ofd.InitialDirectory = @"E:\vsLearn";
            ofd.Multiselect = true;
            ofd.Filter = "文本文件|*.txt|word文件|*.docx";
            ofd.ShowDialog();

            //获得用户选中的文件路径
            string path = ofd.FileName;
            //将文件的全路径存储到泛型集合中
            list.Add(path);
            //获得用户打开文件的文件名
            string fileName = Path.GetFileName(path);
            //将文件名放到ListBox中
            listBox1.Items.Add(fileName);
            if (path == "") {
                return;
            }
            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);

            }
        }
        /// <summary>
        /// 保存对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = @"E:\vsLearn";
            sfd.Title = "请选择要保存的文件路径";
            sfd.Filter = "文本文件|*.txt|word文件|*.docx";
            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);

            }
            MessageBox.Show("保存成功");

        }

        private void 自动换行ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (自动换行ToolStripMenuItem1.Text == "☆自动换行")
            {
                textBox1.WordWrap = true;
                自动换行ToolStripMenuItem1.Text = "★取消自动换行";
            }
            else if (自动换行ToolStripMenuItem1.Text == "★取消自动换行")
            {
                textBox1.WordWrap = false;
                自动换行ToolStripMenuItem1.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 listBox1_DoubleClick(object sender, EventArgs e)
        {
            //获得双击文件对应的全路径
            string path = list[listBox1.SelectedIndex];
            using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                fsRead.Read(buffer,0,buffer.Length);
                textBox1.Text = Encoding.Default.GetString(buffer);
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值