• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#小项目:记事本

武飞扬头像
计算机小混子
帮助1

C#小项目之记事本

子窗体设计

学新通

frmChild.cs

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 textBox1_TextChanged(object sender, EventArgs e)
        {
            toolStripLabelMake.Text = "*";
        }
        //窗体加载事件
        private void frmChild_Load(object sender, EventArgs e)
        {
            //加载时要加载,要加载系统字体
            InstalledFontCollection myFonts = new InstalledFontCollection();
            //获取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 toolStripComboBoxSize_TextChanged(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;
                        //释放资源
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    //保存文件到用户指定的目录
                    //获取用户选择的文件及路径
                    string path = this.Text;
                    //保存文件到指定路径
                    StreamWriter sw = new StreamWriter(path, false);
                    //保存文件到指定路径
                    sw.WriteLine(textBoxNote.Text.Trim());
                    //清理缓存
                    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;
                //通用编码
                StreamReader sr = new StreamReader(path,Encoding.UTF8);
                //读取文档中的数据流
                string text = sr.ReadToEnd();
                textBoxNote.Text = text;
                //将打开的文件路径写在当前窗体的属性中
                this.Text = path;
                //打开文件时清除记号标签
                toolStripLabelMake.Text = "";
                sr.Close();
            }
        }
        //窗体关闭事件
        private void frmChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判断记号label是否时*号
            if(toolStripLabelMake.Text=="*")
            {
                //如果是*进入代码,提示用户尚未保存
                DialogResult dr = MessageBox.Show("文档尚未保存,确定要继续退出吗?","信息询问",MessageBoxButtons.YesNo,MessageBoxIcon.Question); 
                //如果用户选择的是 确定 按钮,
                if(dr == DialogResult.Yes)
                {
                    this.Dispose();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }
        //新建按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBoxNote.Text = "";
            toolStripLabelMake.Text = "";
        }
    }
}

学新通

所有窗体都是Form窗体的派生类

textBox1_TextChanged事件

当变更textBox_Text的内容时进入此函数,在子窗体右上角显示*代表修改过

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

frmChild_Load子窗体加载事件

当子窗体在加载时执行该函数,把系统字体存储在FontFamily数组,方便后续使用

 private void frmChild_Load(object sender, EventArgs e)
        {
            //加载时要加载,要加载系统字体
            InstalledFontCollection myFonts = new InstalledFontCollection();
            //获取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);
            }
            
        }
学新通

toolStripButtonBold_Click鼠标点击加粗事件

点击加粗如果当前是加粗就取消加粗,不是就加粗

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);
            }
                
        }

toolStripButtonItalic_Click鼠标点击倾斜事件

点击倾斜效果如果当前是倾斜就取消倾斜,不是就倾斜

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);
            }
        }

toolStripComboBoxFonts_SelectedIndexChanged改变选择字体的索引事件

改变字体为下拉框中选中的字体

private void toolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
		}

toolStripComboBoxSize_TextChanged改变字体大小事件

改变字体大小为下拉框中选中的字体大小

private void toolStripComboBoxSize_TextChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
        }

toolStripButtonSave_Click点击保存按钮

注释很详细

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;
                        //释放资源
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    //保存文件到用户指定的目录
                    //获取用户选择的文件及路径
                    string path = this.Text;
                    //保存文件到指定路径
                    StreamWriter sw = new StreamWriter(path, false);
                    //保存文件到指定路径
                    sw.WriteLine(textBoxNote.Text.Trim());
                    //清理缓存
                    sw.Flush();
                    sw.Close();
                }
            }
            else
            {
                MessageBox.Show("空文档 不能保存","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
学新通

toolStripButtonOpen_Click点击打开按钮

注释很详细

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

frmChild_FormClosing窗体关闭事件

关闭前没有保存弹出提示框,已经保存就直接关闭,通过判断*

private void frmChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判断记号label是否时*号
            if(toolStripLabelMake.Text=="*")
            {
                //如果是*进入代码,提示用户尚未保存
                DialogResult dr = MessageBox.Show("文档尚未保存,确定要继续退出吗?","信息询问",MessageBoxButtons.YesNo,MessageBoxIcon.Question); 
                //如果用户选择的是 确定 按钮,
                if(dr == DialogResult.Yes)
                {
                    this.Dispose();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }
学新通

toolStripButton1_Click点击新建按钮事件

点击新建子窗口,初始化内容都为空

private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBoxNote.Text = "";
            toolStripLabelMake.Text = "";
        }

frmParent.cs

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 ToolStripMenuItemNew_Click(object sender, EventArgs e)
        {
            //实例化一个子窗体对象
            frmChild child = new frmChild();
            //设置子窗体的父窗体
            child.MdiParent = this;
            //打开子窗体
            child.Show();
        }
        //关闭
        private void ToolStripMenuItemClose_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            frm.Close();
        }
        //关闭全部
        private void ToolStripMenuItemCloseALL_Click(object sender, EventArgs e)
        {
            //this.MdiChildren获取父窗体的子窗体的集合
            foreach (Form form in this.MdiChildren)
            {
                Form frm = this.ActiveMdiChild;
                frm.Close();
            }
        }
        //退出
        private void ToolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

学新通

学新通

ToolStripMenuItemNew_Click点击新建子窗口

点击新建自然要打开一个子窗口,然后设置新打开的子窗口的父窗口为当前窗口,最后显示子窗口

private void ToolStripMenuItemNew_Click(object sender, EventArgs e)
        {
            //实例化一个子窗体对象
            frmChild child = new frmChild();
            //设置子窗体的父窗体
            child.MdiParent = this;
            //打开子窗体
            child.Show();
        }

ToolStripMenuItemClose_Click点击关闭窗口

关闭当前活动的窗口,颜色效果不一样

private void ToolStripMenuItemClose_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            frm.Close();
        }

ToolStripMenuItemCloseALL_Click点击关闭全部

关闭所有打开的窗口

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

ToolStripMenuItemExit_Click点击退出

直接关闭父窗口,所有的子窗口都会关闭

private void ToolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

演示效果

初始

学新通

点击文件中的新建

可以产生多个子窗口,取决于新建了多少个

学新通

点击打开

打开电机驱动.txt

学新通

显示如下

左上角会显示路径

学新通

点击加粗

学新通

点击倾斜

学新通

选择字体

学新通

选择字号

学新通

保存

在内容中输入文字,右上角出现星号,表示没保存

学新通

直接点击×

弹出未保存的提示框

学新通

点击保存在X

可以直接退出

关闭当前激活的窗口

第一个颜色与其他不同,即为当前激活的窗口

学新通

关闭全部窗口

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiifgc
系列文章
更多 icon
同类精品
更多 icon
继续加载