子窗体设计
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窗体的派生类
当变更textBox_Text的内容时进入此函数,在子窗体右上角显示*代表修改过
//编辑文本时
private void textBox1_TextChanged(object sender, EventArgs e)
{
toolStripLabelMake.Text = "*";
}
当子窗体在加载时执行该函数,把系统字体存储在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);
}
}
点击加粗如果当前是加粗就取消加粗,不是就加粗
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_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 = "";
}
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();
}
}
}
点击新建自然要打开一个子窗口,然后设置新打开的子窗口的父窗口为当前窗口,最后显示子窗口
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();
}
可以产生多个子窗口,取决于新建了多少个
打开电机驱动.txt
左上角会显示路径
在内容中输入文字,右上角出现星号,表示没保存
弹出未保存的提示框
可以直接退出
第一个颜色与其他不同,即为当前激活的窗口