C#写一个记事本详细介绍附加代码

一、 项目的介绍
该项目做的是多文档的记事本,在父窗体里能打开多个子窗体,每个子窗体有新建,打开,保存,字体字号,加粗,倾斜,复制,剪切等功能,还可以设置字体的颜色,背景的颜色,也可以实现查找和替换功能。
二、设计过程
1.新建一个记事本项目,删掉原来的FORM1.cs,创建两个新的Windows窗体(需要改主入口点),一个名为Father.cs,另一个为Son.cs,
2.
Son.cs:
先设计子窗体,拖入控件toolstrip,新建12个toolstrip按钮,用来实现12个功能,再新建2个toolStripComboBox1,来实现字体格式和大小,再添加两个Button按钮,用来实现查找和替换操作。拖入TextBox控件,作为文本框。
(1).首先对toolstripbutton,属性选择Image,导入项目资源文件,导入下载的图标,DisplayStyle选择ImageAndText,设置图片和字体都在,TextImageRelation选择ImageAboveText,图片在字体上面,每个Button的Text为新建,打开,保存,全选,复制等等,其他几个同理。TextBox的Multiline属性设为Ture,设置成多行,Dock设置为fill,填满。
(2).字体:选择Load事件,在Load里写窗体加载时来获取系统字体,需要先引入命名空间using System.Drawing.Text;using System.Collections;声明InstalledFontCollection对象加载系统字体,再用一个数组接收,用for循环,用该控件名的Items.Add()函数加载。设置字体SelectedIndexChanged事件
(3). 字号:items里面加字号。设置字号的SelectedIndexChanged事件。
(4).再添加一个toolStripLabel标签,用来判断窗体关闭的时候文档是否已保存,打开文档的时候把label.Text设为空格,如果编辑文档,则把label.Text设为*,编辑窗体关闭事件,如果toolStripLabel.Text值为*,那么就跳出另保存的提示。
(5).双击加粗按钮,编写代码,如果Bold等于false的话就加粗,否则就普通文本。
(6).双击倾斜按钮,编写代码,如果Font.Ltalic等于false,则设置为倾斜,否则就取消。
(7).双击保存按钮,先判断文档内容是否为空,为空就不保存,从工具栏中拖入saveFileDialgo控件,设置保存文档为.txt,用path变量获取用户保存路径,再设置路径,保存之后用Flush()释放缓冲区,用Close()关闭。如果已存在了这个文件则直接保存。
(8). 双击打开按钮,从工具栏中拖入openFileDialgo控件,设置过滤器,只能打开.txt文件,如果ShowDialog()==DialogResult.OK,用path获取打开文件路径,编码采用UTF-8,读完文件中的数据流之后再用.Close()方法关闭。
(9).双击新建按钮,把当前文本框的内容设为空。
(10).全选是用.SelectAll(),复制用.Copy(),剪切用.Cut(),粘贴用.Paste(),撤销用.Undo();
(11).字体和背景是要拖入colorDialog控件,字体的话是ForeColor 等于所选的颜色,背景的话是.BackColor等于所选颜色。
(12).查找:新建一个Form2窗体,把窗体名改为查找,拖进一个Lable标签名为查找内容,拖进一个textBox控件来获取查找内容,两个Button按钮,分别命名为查找下一个和取消,两个radioButton控件,来向上和向下查找,向下checked属性设置为True,checkBox控件用来设置是否区分大小写。
先建立子窗体对象,textBox1_TextChanged事件判断是否有输入,没有输入的话则button1.Enabled应该设置为false,就是不能点击,然后双击查找按钮,先判断是否要区分大小写,如果不区分大小写的话用.ToLower()全转换为小写,搜索范围是主窗口的文本文字,关键词是本窗口文本框文字,再判断是向上查找还是向下查找,向上查找就是从0一开始位置开始,inout=0,找到的话就用Select()选中,未找到就MessageBox.Show(“未找到”)。
(13).替换:新建一个Form3窗体,把窗体名改为替换,先拖入两个Lable控件,Text属性分别改为查找和替换,拖入四个Button控件,Text属性分别改为查找下一个,替换,全部替换,取消,拖入checkBox控件用来设置是否区分大小写。
先实例化子窗体对象,用textBox1_TextChanged事件判断用户是否有输入,如果未输入则把3个按钮的Enabled属性都设为false,不能点击直到textBox有输入,先判断是否要区分大小写,如果不区分大小写的话用.ToLower()全转换为小写,搜索范围是主窗口的文本文字,关键词是本窗口文本框文字,inout=0,找到的话就用Select()选中,未找到就MessageBox.Show(“未找到”)。取消按钮是用了Close();方法,,替换则把查找道德替换掉
3.Father.cs
把Text属性设置为记事本类,把IsMdiContainer设置为true,拖入meueStrip控件进去,添加文件和帮助,在文件中添加新建,关闭,全部关闭,退出,再从工具栏中拖入toolStrip来显示时间,拖入一个timer控件。
新建:实例化一个子窗体对象。
关闭:.Close()关闭当前子窗体对象。
全部关闭:this. MdiChildren获取子窗体集合,再关闭当前所有已打开的。
退出:.Close()退出。
toolStrip新建一个Labe,用来显示系统当前时间,timer的enable属性设置为true,this.toolStripLabel1.Text = DateTime.Now.ToString();来获取当前时间.

详细代码

Father.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 记事本
{
public partial class Father : Form
{
public Father()
{
InitializeComponent();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
//实例化一个子窗体对象
Son son = new Son();
//设置子窗体的父窗体
son.MdiParent = this;
//打开子窗体
son.Show();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form frm = this.ActiveMdiChild;
frm.Close();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form form in this.MdiChildren)
{
Form frm = this.ActiveMdiChild;
frm.Close();
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(“帮助主题”);
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripLabel1.Text = DateTime.Now.ToString();
}
}
}

Son.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 记事本
{
public partial class Son : Form
{
public string mainkey = null; //关键字
public bool MaxOrMin; //区分大小
public Son()
{
InitializeComponent();
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void textBoxnote_TextChanged(object sender, EventArgs e)
{
//编辑文本时
toolStripLabel1.Text = “";
}
private void toolStripComboBox2_Click(object sender, EventArgs e)
{
}
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
}
private void toolStripComboBox1_ForeColorChanged(object sender, EventArgs e)
{
}
private void Son_Load(object sender, EventArgs e)
{
//窗体加载事件,窗体加载时,加载系统字体
InstalledFontCollection myFonts = new InstalledFontCollection();
//获取InstalledFontCollection对象数组
FontFamily[] ff = myFonts.Families;
//声明一个ArrayList变量
ArrayList list = new ArrayList();
//获取系统数组的列表集合长度
int count = ff.Length;
//使用for循环写到控件中
for (int i = 0; i < count; i++)
{
string FontName = ff[i].Name;
toolStripComboBox1.Items.Add(FontName);
}
}
//加粗
private void toolStripButton3_Click(object sender, EventArgs e)
{
if(textBoxnote.Font.Boldfalse)
{
textBoxnote.Font = new Font(textBoxnote.Font, FontStyle.Bold);
}
else
{
textBoxnote.Font = new Font(textBoxnote.Font, FontStyle.Regular);
}
}
private void toolStripButton4_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 toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//改变选择字体的索引事件
string fontname = toolStripComboBox1.Text;
float fontsize = float.Parse(toolStripComboBox2.Text);
textBoxnote.Font = new Font(fontname, fontsize);
}
private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//改变字体大小
string fontname= toolStripComboBox1.Text;
float fontsize = float.Parse(toolStripComboBox2.Text);
textBoxnote.Font = new Font(fontname, fontsize);
}
private void toolStripButton1_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;
//打开文件时,清空记号
toolStripLabel1.Text = “”;
sr.Close();
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
//保存文档
if (textBoxnote.Text.Trim() != “”)
{
//新建一个保存文件的对话框
//创建筛选器/过滤器
saveFileDialog1.Filter = ("文本文档(
.txt)|.txt");
//判断用户是否点击保存按钮还是取消按钮
if(this.Text=="")
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//保存文件到用户指定位置
//获取用户选择的文件及路径
string path = saveFileDialog1.FileName;
//保存文件到指定路径
StreamWriter sw = new StreamWriter(path, false);
sw.WriteLine(textBoxnote.Text.Trim());
toolStripLabel1.Text = “”;
sw.Flush();
sw.Close();
}
}
else
{
string path = this.Text;
StreamWriter sw = new StreamWriter(path, false);
sw.WriteLine(textBoxnote.Text.Trim());
toolStripLabel1.Text = “”;
sw.Flush();
sw.Close();
}
}
else
{
MessageBox.Show(“空文档不能保存”, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Son_FormClosing(object sender, FormClosingEventArgs e)
{
//窗体关闭事件
if(toolStripLabel1.Text=="
”)
{
DialogResult dr= MessageBox.Show(“文件尚未保存,确定要继续”, “信息询问”, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dr==System.Windows.Forms.DialogResult.Yes)
{
this.Dispose();
}
else
{
e.Cancel = true;
}
}
}
private void Son_TextChanged(object sender, EventArgs e)
{
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
//新建按钮
textBoxnote.Text = “”;
toolStripLabel1.Text = “”;
this.Text = “”;
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
textBoxnote.Cut();//剪切
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
textBoxnote.Copy();//复制
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
textBoxnote.Paste();//粘贴
}
private void toolStripButton9_Click(object sender, EventArgs e)
{
textBoxnote.Undo();//撤销
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.mainform = this;
f2.Show();
}
private void toolStripButton10_Click(object sender, EventArgs e)
{
//字体
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBoxnote.ForeColor = colorDialog1.Color;
}
}
private void toolStripButton11_Click(object sender, EventArgs e)
{
//全选
textBoxnote.SelectAll();
}
private void toolStripButton12_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBoxnote.BackColor = colorDialog1.Color;
}
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.mainform = this;
f3.Show();
f3.textBox2.Text = textBoxnote.SelectedText; //查找内容:主窗口选中文本文字
}
}
}
From2.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 记事本
{
public partial class Form2 : Form
{
public Son mainform;
public string textsearching; //被查找的文档
public string keywordsearching; //查找的关键字
public int inout = 0;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0) //若文本框中没有输入文字则‘查找下一个’按钮禁用
button1.Enabled = true;
else
button1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
//区分大小写?
if (checkBox1.Checked) //区分大小写
{
textsearching = mainform.textBoxnote.Text; //搜索范围是主窗口的文本文字
keywordsearching = textBox1.Text;    //关键词是本窗口文本框文字
mainform.mainkey = textBox1.Text;  //主窗口的搜索关键字是本窗口文本框的文字
mainform.MaxOrMin = true;        //主窗口区分大小写标记
}
else
{
textsearching = mainform.textBoxnote.Text.ToLower();//搜索范围是主窗口的文本文字,并将文字转换为小写
keywordsearching = textBox1.Text.ToLower(); //关键词是本窗口文本框文字,并将文字转换为小写
mainform.mainkey = textBox1.Text; //主窗口的搜索关键字是本窗口文本框的文字
mainform.MaxOrMin = false; //主窗口区分大小写标记
}
//向上/向下查找?
if (radioButton1.Checked) //向下查找
{
//确定搜索的范围对应文本的个数
//int temp = textsearching.IndexOf(mainform.mainkey, mainform.textBoxnote.SelectionStart + mainform.textBoxnote.SelectedText.Length);
// int temp = textsearching.IndexOf(mainform.mainkey, mainform.textBoxnote.SelectionStart);
int temp = textsearching.IndexOf(mainform.mainkey, inout);
if (temp >= 0) //有对应文本
{
mainform.textBoxnote.Select(temp, keywordsearching.Length); //主窗口对应文本选中
//mainform.textBoxnote.ScrollToCaret(); //滚动条滚到对应文本位置
mainform.textBoxnote.Focus(); //焦点放在选中文本后面
inout=++temp;
}
else
{
//搜索不到对应文本
MessageBox.Show(“找不到”" + keywordsearching + “”", “记事本”, MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
else //向上查找
{
int temp = -1;
if (mainform.textBoxnote.SelectionStart > 1) //主窗口光标不在文本最前面
//确定搜索范围对应文本的个数
temp = textsearching.LastIndexOf(keywordsearching, mainform.textBoxnote.SelectionStart - 1);
if (temp >= 0) //有对应文本
{
this.mainform.textBoxnote.Select(temp, keywordsearching.Length); //主窗口对应的文本选中
this.mainform.textBoxnote.ScrollToCaret(); //滚动条滚到对应文本位置
this.mainform.Focus(); //焦点放在选中文本后面
}
else
{
//搜索不到对应文本
MessageBox.Show(“找不到”" + keywordsearching + “”", “记事本”, MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
}
}

From3.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 记事本
{
public partial class Form3 : Form
{
public Son mainform;
public string textsearching;  //被查找的文本
public string keywordsearching; //查找的关键词
int begin = 0;
public int inout = 0;
public Form3()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
int temp = -1;
int i = 0;
if (checkBox1.Checked) //区分大小写
{
textsearching = this.mainform.textBoxnote.Text; //搜索范围是主窗口的文字
keywordsearching = textBox1.Text; //关键词是本窗口文本框文字
}
else
{
textsearching = this.mainform.textBoxnote.Text.ToLower(); //搜索范围是主窗口的文字,并转换成小写
keywordsearching = textBox1.Text.ToLower(); //关键词是本窗口文本框文字,并转换成小写
}
temp = textsearching.IndexOf(keywordsearching, 0); //从文本头开始搜索
if (temp > 0)
{
this.mainform.textBoxnote.Select(temp, keywordsearching.Length); //选中主窗口相符的文字
button2.PerformClick(); //替换
this.mainform.textBoxnote.Select(temp, textBox2.Text.Length);
i++;
while (true) //循环判断
{
begin = this.mainform.textBoxnote.SelectionStart + textBox2.Text.Length;
temp = textsearching.IndexOf(keywordsearching, begin);
if (temp >= 0) //找到对应关键字
{
this.mainform.textBoxnote.Select(temp, keywordsearching.Length); //选中主窗口相符的文字
button2.PerformClick(); //替换
this.mainform.textBoxnote.Select(temp, textBox2.Text.Length);
i++;
}
else
{
this.mainform.textBoxnote.ScrollToCaret(); //滚动条滚到合适位置
break;
}
}
if (textBox1.Text.Length > textBox2.Text.Length)
{
temp = textsearching.IndexOf(keywordsearching, begin + textBox2.Text.Length - textBox1.Text.Length - 1);
this.mainform.textBoxnote.Select(temp, keywordsearching.Length); //选中主窗口相符的文字
button2.PerformClick(); //替换
this.mainform.textBoxnote.Select(temp, textBox2.Text.Length);
i++;
}
}
MessageBox.Show(“一共替换了” + i + “个位置”, “替换结果”, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//按钮控制器
if (textBox1.Text.Length > 0)   //文本框中有输入时激活按键
{
button1.Enabled = true;          //查找下一个
button2.Enabled = true;          //替换
button3.Enabled = true;          //全部替换
}
else
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)                    //区分大小写
{
textsearching = this.mainform.textBoxnote.Text;        //搜索内容为主窗口文本
keywordsearching = textBox1.Text;             //搜索关键词是本窗口文本框文本
}
else
{
textsearching = this.mainform.textBoxnote.Text.ToLower();   //搜索内容为主窗口文本,并将文本转换为小写
keywordsearching = textBox1.Text.ToLower();        //搜索关键词是本窗口文本框文本,并将文本转换为小写
}
//指定范围内被查找关键字的个数
//int temp = textsearching.IndexOf(keywordsearching, mainform.textBoxnote.SelectionStart + mainform.textBoxnote.SelectedText.Length);
int temp = textsearching.IndexOf(keywordsearching, inout);
if (temp >= 0)   //有关键字
{
this.mainform.textBoxnote.Select(temp, keywordsearching.Length);//主窗口选中该关键字
this.mainform.textBoxnote.ScrollToCaret();           //滚动条滚到对应位置
this.mainform.Focus(); //焦点放在选中文字后面
inout = ++temp;
}
else
{
//找不到关键字
if (begin == 0)
MessageBox.Show(“找不到”" + keywordsearching + “”", “提示”, MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == this.mainform.textBoxnote.SelectedText)
{
this.mainform.textBoxnote.SelectedText = textBox2.Text;
}
else
{
button1.PerformClick();
this.mainform.textBoxnote.SelectedText = textBox2.Text;
}
}
private void button4_Click(object sender, EventArgs e)
{
Close();
}
}
}

运行效果图

在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Mickey记事本 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // 用于存储当前操作的文件的名称 private string textFileName = ""; private string filePath = ""; private void 新建_Click(object sender, EventArgs e) { textFileName = ""; // 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存” if (inputInfo.Text != string.Empty) { // 若用户选择“是”,应弹出保存文件的对话框 if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) == DialogResult.Yes) { // 保存文件 Save(); Text = "新建-Mickey记事本"; inputInfo.Text = ""; } else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) == DialogResult.No) { // 用户选择不保存时将输入框中的内容清除 inputInfo.Text = ""; } } } private void 打开_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "文本文件(*.txt)|*.txt"; if (openFile.ShowDialog() == DialogResult.OK) { StreamReader sr = new StreamReader(openFile.FileName); inputInfo.Text = sr.ReadToEnd(); sr.Close(); FileInfo fileInfo = new FileInfo(openFile.FileName); // 把标题改为打开的文件的名称 Text = "*" + fileInfo.Name + "-Mickey记事本"; textFileName = fileInfo.Name; } } private void 保存_Click(object sender, EventArgs e) { Save(); } // “保存” private void Save() { if (!textFileName.Equals("")) { SaveFileDialog saveFile = new SaveFileDialog(); // 默认保存格式 saveFile.Filter = "文本文件(*.txt)|*.txt"; StreamWriter sw = new StreamWriter(filePath, false); sw.Write(inputInfo.Text); sw.Close(); MessageBox.Show("文件保存成功!", "Mickey温馨提示"); filePath = saveFile.FileName; // 把标题改为打开的文件的名称 Text = textFileName + "-Mickey记事本"; } else { // 成员变量为“”,说明文件第一次保存,执行“另存为”操作 HoldFile(); } } private void HoldFile() { // 若用户选择另保存文件 SaveFileDialog saveFile = new SaveFileDialog(); // 默认保存格式 saveFile.Filter = "文本文件(*.txt)|*.txt"; if (saveFile.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFile.FileName, false); sw.WriteLine(inputInfo.Text); sw.Close(); MessageBox.Show("文件保存成功!", "Mickey温馨提示"); filePath = saveFile.FileName; } // 判断是第一次保存还是第二次 if (textFileName.Equals("")) { FileInfo fileInfo = new FileInfo(saveFile.FileName); Text = fileInfo.Name + "-Mickey记事本"; textFileName = fileInfo.Name; } else { // 把标题改为打开的文件的名称 Text = textFileName + "-Mickey记事本"; } } private void 另存为_Click(object sender, EventArgs e) { HoldFile(); } private void 页面设置_Click(object sender, EventArgs e) { this.pageSetupDialog1.Document = this.printDocument1; pageSetupDialog1.ShowDialog(); } private void 打印_Click(object sender, EventArgs e) { if (inputInfo.Text.Length < 1) { MessageBox.Show("请确保要打印的文件的内容不为空!", "Mickey温馨提示"); return; } else { // 设置Document的属性 this.printDialog1.Document = this.printDocument1; this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings; if (this.printDialog1.ShowDialog() == DialogResult.OK) { try { this.printDocument1.Print(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } private void 退出_Click(object sender, EventArgs e) { // 退出时应提示用户是否保存当前文本文件 DialogResult result = MessageBox.Show("是否将更改保存?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); if (result == DialogResult.Yes) { Save(); Application.Exit(); } else if (result == DialogResult.No) { Application.Exit(); } } // 这个成员变量用来存储用户选择进行操作的字符串 private string selectedInfo = ""; private void 编辑_Click(object sender, EventArgs e) { if ((inputInfo.SelectedText.Equals("")) && (selectedInfo.Equals(""))) { 剪切.Enabled = false; 复制.Enabled = false; 粘贴.Enabled = false; 删除.Enabled = false; } else { 剪切.Enabled = true; 复制.Enabled = true; 粘贴.Enabled = true; 删除.Enabled = true; } } private void 撤销_Click(object sender, EventArgs e) { this.inputInfo.Undo(); } private void 剪切_Click(object sender, EventArgs e) { selectedInfo = inputInfo.SelectedText; this.inputInfo.Cut(); } private void 复制_Click(object sender, EventArgs e) { this.inputInfo.Copy(); } private void 粘贴_Click(object sender, EventArgs e) { this.inputInfo.Paste(); } private void 删除_Click(object sender, EventArgs e) { this.inputInfo.SelectedText = ""; } private void 查找_Click(object sender, EventArgs e) { if (inputInfo.Text == string.Empty) { MessageBox.Show("请确保要查找的文件的内容不为空!", "Mickey温馨提示"); } else { //Form2 fr2 = new Form2(); //fr2.sender(this); //fr2.Show(); } } private void 查找下一个_Click(object sender, EventArgs e) { } private void 全选_Click(object sender, EventArgs e) { this.inputInfo.SelectAll(); //全选_Click(sender,e); } private void 时间日期_Click(object sender, EventArgs e) { inputInfo.Text += "现在时间是:" + DateTime.Now.ToString(); } private void 自动换行_Click(object sender, EventArgs e) { if (自动换行.Checked == true) { inputInfo.WordWrap = true; } else { inputInfo.WordWrap = false; } } private void 字体_Click(object sender, EventArgs e) { FontDialog fontDialog = new FontDialog(); if (fontDialog.ShowDialog() == DialogResult.OK) { inputInfo.Font = fontDialog.Font; } } private void 查看_Click(object sender, EventArgs e) { if (inputInfo.Text.Length > 0) { 状态栏.Enabled = true; } else { 状态栏.Enabled = false; } } private void 状态栏_Click(object sender, EventArgs e) { if (状态栏.Checked == true) { 状态栏.Checked = false; statusStrip1.Visible = false; } else { 状态栏.Checked = true; statusStrip1.Visible = true; } } private void 查看帮助_Click(object sender, EventArgs e) { string help = @"C:\Users\狗狗Mickey\Desktop\help.txt"; Help.ShowHelp(this, help); } private void 关于记事本_Click(object sender, EventArgs e) { AboutBox1 about = new AboutBox1(); about.Show(); } } }
以下是一个简单的记事本功能的 C# 代码示例: ``` using System; using System.IO; using System.Windows.Forms; namespace Notepad { public partial class Form1 : Form { string filePath = ""; public Form1() { InitializeComponent(); } private void NewFile() { if (textBox1.Text.Length > 0) { DialogResult result = MessageBox.Show("Do you want to save changes to this file?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Save(); } else if (result == DialogResult.Cancel) { return; } } textBox1.Text = ""; filePath = ""; } private void Open() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { using (StreamReader reader = new StreamReader(openFileDialog.FileName)) { textBox1.Text = reader.ReadToEnd(); filePath = openFileDialog.FileName; } } } private void Save() { if (filePath.Length > 0) { using (StreamWriter writer = new StreamWriter(filePath)) { writer.Write(textBox1.Text); } } else { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { writer.Write(textBox1.Text); filePath = saveFileDialog.FileName; } } } } private void SaveAs() { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { writer.Write(textBox1.Text); filePath = saveFileDialog.FileName; } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (textBox1.Text.Length > 0) { DialogResult result = MessageBox.Show("Do you want to save changes to this file?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Save(); } else if (result == DialogResult.Cancel) { e.Cancel = true; } } } private void newToolStripMenuItem_Click(object sender, EventArgs e) { NewFile(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { Open(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { Save(); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { SaveAs(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } } } ``` 这个记事本应用程序基于 Windows 窗体,包括菜单栏、工具栏和一个文本框用于编辑文本。它支持新建、打开、保存、另存为和退出功能。它使用 OpenFileDialog 和 SaveFileDialog 对话框来打开和保存文件。在关闭应用程序时,如果存在未保存的更改,会提示用户保存更改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值