用C#写一个记事本

记事本样式 【注:最后附上网盘链接源代码】

在这里插入图片描述

总起

记事本总共有两个窗体,分别是父窗体和子窗体,子窗体实质上就是记事本


父窗体

其中父窗体的功能有四个,分别是①新建一个子窗体,②关闭一个子窗体,③关闭所有子窗体以及④退出。

父窗体代码如下:

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

    namespace Notepad
    {
        public partial class FrmParent : Form
        {
            public FrmParent()
            {
                InitializeComponent();
            }

            //新建
            private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //实例化一个子窗体对象
                FrmChild child = new FrmChild();
                //设置子窗体的父窗体
                child.MdiParent = this;
                //打开子窗体
                child.Show();
            }
    
            //关闭
            private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form frm = this.ActiveMdiChild;
                frm.Close();
            }

            //关闭全部
            private void 关闭全部ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //this.MdiChildren获取父窗体的子窗体集合
                foreach(Form form in this.MdiChildren)
                {
              //      Form frm = this.ActiveMdiChild;
                    form.Close();
                }
            }

            //退出
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }

子窗体

子窗体拥有七个大功能,分别是①新建一个记事本,②打开一个记事本,③保存一个记事本,④对记事本中的文字加粗,⑤对记事本中的文字倾斜,⑥调节字体和⑦字号。
子窗体代码如下:

using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;    
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    //命名空间
    using System.Drawing.Text;
    using System.Collections;
    using System.IO;

    namespace Notepad
    {
    public partial class FrmChild : Form
    {
        public FrmChild()
        {
            InitializeComponent();
        }

        //窗体加载事件
        private void FrmChild_Load(object sender, EventArgs e)
        {
            //窗体加载时,要加载系统字体
            InstalledFontCollection myFonts = new InstalledFontCollection();
            //获取系统字体的数组
            FontFamily[] ff = myFonts.Families;
            //声明一个ArrayList的变量
            ArrayList list = new ArrayList();
            //获取系统数组的列表中集合的长度
            int count = ff.Length;
            //使用for循环把字体名称写入到 toolStripComboBoxFonts 控件中
            for (int i = 0; i < count; i++)
            {
                string FontName = ff[i].Name;
                toolStripComboBoxFonts.Items.Add(FontName);
            }
        }

        //加粗按钮
        private void toolStripButtonBold_Click(object sender, EventArgs e)
        {
            //点击按钮加粗,加粗时再点击按钮取消加粗
            if (textBoxNote.Font.Bold == false)
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Bold);
            }
            else
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular);
            }
            
        }

        //倾斜按钮
        private void toolStripButtonItalic_Click(object sender, EventArgs e)
        {
            //点击按钮倾斜,加粗时再点击按钮取消倾斜
            if (textBoxNote.Font.Italic == false)
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Italic);
            }
            else
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular);
            }
        }

        //改变选择字体的索引时间
        private void toolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
        }

        //改变字体大小
        private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
        }

        //保存文档
        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            if (textBoxNote.Text.Trim() != "")
            {
                if (this.Text == "")
                {
                    //新建一个保存文件的对话框
                    //创建一个过滤器
                    saveFileDialog1.Filter = ("文本文档(*.txt)|*.txt");
                    //判断用户点击的是保存按钮还是取消按钮
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        //保存文件到用户指定的目录位置
                        //获取用户选择的文件及路径
                        string path = saveFileDialog1.FileName;//自己选路径
                        //保存(写入)文件到指定路径
                        StreamWriter sw = new StreamWriter(path, false);
                        sw.WriteLine(textBoxNote.Text.Trim());
                        //把窗体text属性设置为保存后的路径
                        this.Text = path;
                        //标记改为已保存
                        toolStripLabelMark.Text = "已保存";
                        //释放资源
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    //保存文件到用户指定的目录位置
                    //获取用户选择的文件及路径
                    string path = this.Text;//自己选路径
                    //保存(写入)文件到指定路径
                    StreamWriter sw = new StreamWriter(path, false);
                    sw.WriteLine(textBoxNote.Text.Trim());
                    //标记改为已保存
                    toolStripLabelMark.Text = "已保存";
                    //释放资源
                    sw.Flush();
                    sw.Close();
                }
            }
            else
            {
                MessageBox.Show("空文档不能保存", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //打开文档
        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            //新建一个保存文件的对话框
            //创建一个过滤器
            openFileDialog1.Filter = ("文本文档(*.txt)|*.txt");
            //判断用户点击的是保存按钮还是取消按钮
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //获取打开文档的路径
                string path = openFileDialog1.FileName;
                //通用编码UTF8,读取文档中的数据流
                StreamReader sr = new StreamReader(path, Encoding.UTF8);
                string text = sr.ReadToEnd();
                textBoxNote.Text = text;
                //将打开的文件路径写到当前窗体的text属性中
                this.Text = path;
                //打开文件时,清空记号标签
                toolStripLabelMark.Text = "";
                sr.Close();
            }
        }

        //编辑文本时
        private void textBoxNote_TextChanged(object sender, EventArgs e)
        {
            toolStripLabelMark.Text = "*";
        }

        //窗体关闭事件
        private void FrmChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判断记号Label是否是*号 *号代表修改过
            if (toolStripLabelMark.Text == "*")
            {
                //如果是*号,进入代码,提示用户尚未保存
                DialogResult dr = MessageBox.Show("文档尚未保存,确定要继续退出吗?", "信息询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                //如果用户选择的是确定按钮
                if (dr == DialogResult.Yes)
                {
                    this.Dispose();
                }
                else
                {
                    e.Cancel = true;
                }
            }

        }

        //新建按钮
        private void toolStripButtonNew_Click(object sender, EventArgs e)
            {
            textBoxNote.Text = "";
            toolStripLabelMark.Text = "";
            this.Text = "";
            }
         }
    }

网盘链接

https://pan.baidu.com/s/10HO-C7tRryXPMzqGtf6k2Q?pwd=r66h

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的记事本功能的 C# 代码示例: ``` using System; using System.IO; using System.Windows.Forms; namespace Notepad { public partial class Form1 : Form { string filePath = ""; public Form1() { InitializeComponent(); } private void NewFile() { if (textBox1.Text.Length > 0) { DialogResult result = MessageBox.Show("Do you want to save changes to this file?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Save(); } else if (result == DialogResult.Cancel) { return; } } textBox1.Text = ""; filePath = ""; } private void Open() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { using (StreamReader reader = new StreamReader(openFileDialog.FileName)) { textBox1.Text = reader.ReadToEnd(); filePath = openFileDialog.FileName; } } } private void Save() { if (filePath.Length > 0) { using (StreamWriter writer = new StreamWriter(filePath)) { writer.Write(textBox1.Text); } } else { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { writer.Write(textBox1.Text); filePath = saveFileDialog.FileName; } } } } private void SaveAs() { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { writer.Write(textBox1.Text); filePath = saveFileDialog.FileName; } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (textBox1.Text.Length > 0) { DialogResult result = MessageBox.Show("Do you want to save changes to this file?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Save(); } else if (result == DialogResult.Cancel) { e.Cancel = true; } } } private void newToolStripMenuItem_Click(object sender, EventArgs e) { NewFile(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { Open(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { Save(); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { SaveAs(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } } } ``` 这个记事本应用程序基于 Windows 窗体,包括菜单栏、工具栏和一个文本框用于编辑文本。它支持新建、打开、保存、另存为和退出功能。它使用 OpenFileDialog 和 SaveFileDialog 对话框来打开和保存文件。在关闭应用程序时,如果存在未保存的更改,会提示用户保存更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值