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循环把字体名称写入到toolStripComboBoxFontsfor (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循环把字体名称写入到toolStripComboBoxFontsfor (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
可以直接退出
关闭当前激活的窗口
第一个颜色与其他不同,即为当前激活的窗口