介绍
本记事本使用Visual Studio2019编辑,以学习为主,故功能尚未全完善,主要功能有新建、打开、保存、加粗、斜体、字体、大小、复制、粘贴、全选、删除、剪切、查找、替换、日期以及父子窗口控制
记事本设计
创建
- 窗体应用.NET Framework
- 点击想要的控件直接拖动
- 在属性里调制所选中的控件样式
设计里面的name用于编写代码时调用的变量名,外观Text文本“文件”。
父子窗口
-在Program.cs中设置FrmParents(已经建好)为主入口点-建立俩窗口的父子关系
双击窗体内控件按钮或窗体即可进入代码模块
主要代码:
//新建
private void ToolStripMenuItemNew_Click(object sender, EventArgs e)
{
//实例化子窗体
FrmChild ch = new FrmChild();
//设置子窗体的父窗体
ch.MdiParent = this;
//打开子窗体
ch.Show();
}
//关闭
private void ToolStripMenuItemClose_Click(object sender, EventArgs e)
{
//关闭处于活动状态的子窗体
Form frm = this.ActiveMdiChild;
frm.Close();
}
//关闭全部
private void ToolStripMenuItemCloseAll_Click(object sender, EventArgs e)
{//循环关闭
foreach (Form form in this.MdiChildren)
{
Form frm = this.ActiveMdiChild;
frm.Close();
}
}
//退出
private void ToolStripMenuItemExit_Click(object sender, EventArgs e)
{
this.Close();
}
子窗口(记事本)
-我设计的亚子(不好看,仅满足学习需要),自己需要关注的是设计里的name(灵活设置)
- 直接上代码
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();
//获取InstalledFontCollection字体对象数组
FontFamily[] ff = myFonts.Families;
//声明可变数组
ArrayList list = new ArrayList();
//获取集合长度
int count = ff.Length;
//将字体名字存入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 (searchBoxNote.Font.Bold)
{
searchBoxNote.Font = new Font(searchBoxNote.Font, FontStyle.Regular);
}
else
{
searchBoxNote.Font = new Font(searchBoxNote.Font, FontStyle.Bold);
}
}
//斜体按钮
private void toolStripButtonItalic_Click(object sender, EventArgs e)
{
//判断斜体更改状态
if (searchBoxNote.Font.Italic)
{
searchBoxNote.Font = new Font(searchBoxNote.Font, FontStyle.Regular);
}
else
{
searchBoxNote.Font = new Font(searchBoxNote.Font, FontStyle.Italic);
}
}
//改变字体family
private void toolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e)
{
String fontName = toolStripComboBoxFonts.Text;
float fontSize = float.Parse(toolStripComboBoxSize.Text);
searchBoxNote.Font = new Font(fontName, fontSize);
}
//改变字体大小
private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
{
String fontName = toolStripComboBoxFonts.Text;
float fontSize = float.Parse(toolStripComboBoxSize.Text);
searchBoxNote.Font = new Font(fontName, fontSize);
}
//保存文档
private void toolStripButtonSave_Click(object sender, EventArgs e)
{
if (searchBoxNote.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(searchBoxNote.Text.Trim());
sw.Flush();
sw.Close();
}
}
else
{
//获取用户选择的文件和路劲
String path = this.Text;
//保存到指定路劲
StreamWriter sw = new StreamWriter(path, false);
sw.WriteLine(searchBoxNote.Text.Trim());
//把窗体text属性设为保存后的路径
this.Text = path;
sw.Flush();
sw.Close();
}
toolStripLabel.Text = "已保存状态";
}
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;
//通用编码utf-8
StreamReader sr = new StreamReader(path, Encoding.UTF8);
//读取文档中的数据流
String text = sr.ReadToEnd();
//数据流存入当前noteText
searchBoxNote.Text = text;
//路劲存入note.Text
this.Text = path;
//打开文件时清空记号
toolStripLabel.Text = "未编辑状态";
sr.Close();
}
}
//编辑文本时
private void textBoxNote_TextChanged(object sender, EventArgs e)
{
toolStripLabel.Text = "未保存状态";
}
//窗体关闭事件
private void FrmChild_FormClosing(object sender, FormClosingEventArgs e)
{
//判断是否更改过
if(toolStripLabel.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)
{
searchBoxNote.Text = "";
toolStripLabel.Text = "未编辑状态";
this.Text = "";
}
//撤销状态
private void toolStripButtonUndo_Click(object sender, EventArgs e)
{
searchBoxNote.Undo();
}
}
}
遇到的问题以及解决方法
有时候不小心多点了一下,新建了无用的函数,删除之后就报错
删除后报错:
点进’错误实例’删掉红线底的代码即可