⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 用C#实现了计事本的功能 对初学者相当有用的
💻 CS
📖 第 1 页 / 共 2 页
字号:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Media;

    namespace jishiben
    {
        public partial class Form1 : Form
        {
            private SoundPlayer Player = new SoundPlayer();
            string filePath = null; //判断文件默认保存的路径
            int zijie=0;//用于判断文件是否更改
            bool df = false;//此变量用于判断 是否应该显示状态栏
          
            public Form1()
            {
                InitializeComponent();
            }
           

            private void Form1_Load(object sender, EventArgs e)
            {
                skinEngine1.SkinFile = "DiamondBlue.ssk";

                this.textShow.WordWrap = false;
                try
                {
                      //this.Player.SoundLocation = "1.wav";
                      //              this.Player.PlayLooping();
                }catch(Exception r)
                {
                    MessageBox.Show(r.Message);
                }

               
            }

            private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (zijie != this.textShow.Text.Length)
                {
                    DialogResult result = MessageBox.Show("文件" + filePath + "已发生改变是否保存", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);

                    if (result == DialogResult.Yes)
                    {
                        保存ToolStripMenuItem_Click(this, null);
                    }
                    if (result == DialogResult.No)
                    {
                        //**********************************
                        //Application.Exit();关闭
                    }
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                   

                }
                openFileDialog1.Title = "打开";
             //  openFileDialog1.Filter = "文本文件(*.txt)|*.txt";//文件过滤显示

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {

                    string fileName = openFileDialog1.FileName;
                    FileStream fs = new FileStream(fileName, FileMode.Open);
                    filePath = fileName;//**************************
                    try
                    {

                        StreamReader reader = new StreamReader(fs);

                        string strXinxi = reader.ReadToEnd();//ReadtoEnd一次读完ReadLine//读一行
                        textShow.Text = strXinxi;
                        reader.Close();
                    }
                    catch (Exception es)
                    {
                        MessageBox.Show(es.Message);
                    }
                    finally
                    {

                        fs.Close();
                    }
                }
                else
                {
                    return;
                }
                    zijie = textShow.Text.Length;

              }



            private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                colorDialog1.ShowDialog();
                textShow.ForeColor = colorDialog1.Color;
            }

            private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                saveFileDialog1.DefaultExt = "txt";//设置文件默认扩展名
                saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string lujin = saveFileDialog1.FileName;
                    FileStream fs = new FileStream(lujin, FileMode.Create);

                    try
                    {
                        string inshow = textShow.Text;
                        UTF8Encoding encodeing = new UTF8Encoding(true);
                        byte[] by = encodeing.GetBytes(inshow);
                        fs.Write(by, 0, by.Length);
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        fs.Flush();
                        fs.Close();
                    }
                }
                else
                {
                    return;
                }
                zijie = textShow.Text.Length;
            }

            private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                fontDialog1.ShowDialog();
                Font f = fontDialog1.Font;
                textShow.Font = f;
            }

            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

            private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textShow.SelectAll();
            }

            private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (filePath == null)
                {
                    另存为ToolStripMenuItem_Click(this, null);
                  
                }
                else
                {
                    FileStream fs = new FileStream(filePath, FileMode.Create);

                    try
                    {
                        string inshow = textShow.Text;
                        UTF8Encoding encodeing = new UTF8Encoding(true);
                        byte[] by = encodeing.GetBytes(inshow);
                        fs.Write(by, 0, by.Length);
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        fs.Flush();
                        fs.Close();
                    }
                }
                zijie = textShow.Text.Length;
            }

            private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (textShow.Text == "" || textShow.Text == null || zijie == this.textShow.Text.Length)
                {
                    textShow.Text = "";
                    filePath = null;
                }
                else
                {
                    DialogResult result = MessageBox.Show("文件" + filePath + "已发生改变是否保存", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);

                    if (result == DialogResult.Yes)
                    {
                        if (filePath != null)
                        {
                            FileStream fs = new FileStream(filePath, FileMode.Create);

                            try
                            {
                                string inshow = textShow.Text;
                                UTF8Encoding encodeing = new UTF8Encoding(true);
                                byte[] by = encodeing.GetBytes(inshow);
                                fs.Write(by, 0, by.Length);
                            }
                            catch (Exception er)
                            {
                                MessageBox.Show(er.Message);
                            }
                            finally
                            {
                                fs.Flush();
                                fs.Close();
                            }
                        }
                        else
                        {
                            另存为ToolStripMenuItem_Click(this, null);
                            return;//如果另存则不获取当前字节信息

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -