c# 简易笔记本 ,对话框

 

form1:

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 简易记事本
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        /// <summary>
        /// 全局变量,当前打开的文件路径储存在隐藏的marklocationlabel1.text中
        /// </summary>
        filesoperator fop = new filesoperator();//文件操作类
        Dictionary<string, string> Dc = new Dictionary<string, string>();//<路径名,路径>字典

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件:";
            ofd.Filter = "文本文件|*.txt|所有文件|*.txt";
            ofd.Multiselect = false;
            ofd.InitialDirectory = @"G:\"; //初始目录
            ofd.ShowDialog();
            //marklocationlabel1.text中的路径总是最新的这一次
            
            fop.ReadFile(ofd.FileName, this.textBox1,marklocation);

            //判断路径是否为空,去重后加入DC,空则不管

            if (marklocation.Text != null && marklocation.Text != "")
            {
                if (Dc.ContainsValue(marklocation.Text))
                {

                }
                else
                {
                    Dc.Add(Path.GetFileName(marklocation.Text), marklocation.Text);
                }

            }

            //将dc文件名加入listbox,必须先clear()

            listBox1.Items.Clear(); 
            foreach (var item in Dc)
            {
                listBox1.Items.Add(item.Key);
            }
           

        }
    
        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            FontDialog fd = new FontDialog();
            fd.Font = new Font("宋体", 13.8f, FontStyle.Regular);//字体初始化设置
            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 Form1_Load(object sender, EventArgs e)
        {
            listBox1.Visible = false; //form1加载时隐藏listbox
        
        }
        
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //新文件路径为空,打开对话框保存,老文件直接保存
            if (marklocation.Text==null | marklocation.Text == "")
            { 
                
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "文本文件|*.txt";
                sfd.ShowDialog();
                fop.WriteFile(sfd.FileName, textBox1.Text, toolStripStatusLabel1);
            }
            else { 
            fop.WriteFile(marklocation.Text, textBox1.Text,toolStripStatusLabel1);
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "文本文件|*.txt";
            sf.ShowDialog();
            fop.WriteFile(sf.FileName, this.textBox1.Text,toolStripStatusLabel1);

        }


    
        private void timer1_Tick(object sender, EventArgs e)
        {
            //60s为一个保存周期,60s时新文件提示启用,打开的文件自动保存,其他时间再分2种情况
            if (DateTime.Now.Second % 60 == 0)
            {

                if (marklocation.Text != "" | marklocation.Text != null)
                {
                 
                    fop.WriteFile(marklocation.Text, this.textBox1.Text, toolStripStatusLabel1);
                    toolStripStatusLabel1.Text = "自动保存成功!";
                }
                else
                {
                    toolStripStatusLabel1.Text = "打开文件后启用自动保存!";
                }


            }
            else if(marklocation.Text == "" | marklocation.Text == null)
          
            {
                
                toolStripStatusLabel1.Text = "打开文件后启用自动保存!";
            }
            else
            {
                toolStripStatusLabel1.Text = "自动保存倒计时中---" + (60 - Convert.ToInt32(DateTime.Now.Second));
            }

        }

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

        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            listBox1.Visible = false; //双击隐藏listbox
        }

        private void 历史ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.Visible == true)
            {
                listBox1.Visible = false;
            }
            else
            {
                listBox1.Visible = true;
            }

        }

        //随着listbox选择不同项,当前项先保存,再打开新的项文件
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            fop.WriteFile(marklocation.Text, textBox1.Text, toolStripStatusLabel1);
            fop.ReadFile(Dc[listBox1.SelectedItem.ToString()], this.textBox1,marklocation);
 
        }

        //双击隐藏切换
        private void label1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.Visible == true)
            {
                listBox1.Visible = false;
            }
            else
            {
                listBox1.Visible = true;
            }
        }

        private void 版本说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string updatehistory = "2021.4.25 实现自动保存,listbox列表" + "\t\r" +  
                "2021.";
            
            MessageBox.Show(updatehistory, "", MessageBoxButtons.OK);

        }

        private void 待实现功能ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newfunction = "添加设置选项,长久保存" + "\t\r";
                
            MessageBox.Show(newfunction, "", MessageBoxButtons.OK);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

  fileoperator文件操作类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 简易记事本
{
     class filesoperator
    {   
       public string Text { get ; set; }

       /// <summary>
       /// 读取文件
       /// </summary>
       /// <param name="path">文件路径</param>
       /// <param name="textBox1">写入的文本框对象</param>
       /// <param name="marklocation">读取的路径存入label.text中</param>
        public void ReadFile(string path,TextBox textBox1,Label marklocation)
        {

            if (path== "")
            {
                return ; //路径空则跳出方法
            }
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] fbytes = new byte[1024 * 1024 * 5];
                int realcount=fs.Read(fbytes, 0, fbytes.Length);
                string textcont= Encoding.Default.GetString(fbytes, 0, realcount);

                textBox1.Text= textcont;
                marklocation.Text = path; 
                
            }
            
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="contents">内容</param>
        /// <param name="toolStripStatusLabel">写入的状态栏</param>
        public void WriteFile(string path,string contents, ToolStripStatusLabel toolStripStatusLabel)
        {
            if (path == ""|path==null)
            {
                return;
            }
            using (FileStream fs=new FileStream(path,FileMode.Create) )
            {
                byte[] filebytes = Encoding.Default.GetBytes(contents);
                fs.Write(filebytes, 0, filebytes.Length);
            }

            toolStripStatusLabel.Text = "写入成功!";
        }

        }


    }

  

 

posted @ 2021-04-25 23:02  遥月  阅读(181)  评论(0)    收藏  举报