记事本样式 【注:最后附上网盘链接源代码】
总起
记事本总共有两个窗体,分别是父窗体和子窗体,子窗体实质上就是记事本
父窗体
其中父窗体的功能有四个,分别是①新建一个子窗体,②关闭一个子窗体,③关闭所有子窗体以及④退出。
父窗体代码如下:
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