作业3:使用C#编写一个记事本
一、功能说明
编写一个C#程序实现记事本的各项功能。
如文件的新建、打开、保存功能;文字的复制、粘贴、删除功能;字体类型、格式的设置功能;查看日期时间等,并且用户可以根据需要显示或者隐藏工具栏和状态栏。
效果图

二、界面设计
二:界面设计
(1)MenuStrip(菜单控件)。
单击窗体左边会出现工具箱→ 找到menustrip →拖动到窗体中
单击菜单栏→右下角属性→修改Name属性为“mnusNotepad”
单击mnusNotepad输入(文件(F)、编辑(E)、格式(O)、查看(V)、帮助(H))
单击文件(F)输入(新建(N) Ctrl+N、打开(O) Ctrl+O、保存(S) Ctrl+S、另存为(A)、退出(X))
单击编辑(E)输入(撤销(V) Ctrl+Z、复制© Ctrl+C、剪切(T) Ctrl+X、粘贴§ Ctrl+V、全选(A) Ctrl+A、日期(D) F5)
单击格式(O)输入(自动换行(W)、字体(F))
单击查看(V)输入(工具栏(T)、状态栏(S))
单击帮助(H)输入(关于记事本(A))
属性设置:
Text属性 Name属性 ShortcutKeys属性 ShowShortcutKeys属性
文件(F) tsmiFile None -
新建(N) tsmiNew Ctrl+N True
打开(O) tsmiOpen Ctrl+O True
保存(S) tsmiSave Ctrl+S True
另存为(A) tsmiSaveAs None -
分隔符(用“-”来分隔)
退出(X) tsmiClose None -
Text属性 Name属性 ShortcutKeys属性 ShowShortcutKeys属性
编辑(E) tsmiEdit None -
撤销(U) tsmiUndo Ctrl+Z True
分隔符
复制(C) tsmiCopy Ctrl+C True
剪切(T) tsmiCut Ctrl+X True
粘贴(P) tsmiPaste Ctrl+V True
分隔符
全选(A) tsmiSelectAll Ctrl+A True
日期(D) tsmiDate F5 True
Text属性 Name属性 Checked属性
格式(O) tsmiFormat False
自动换行(W) tsmiAuto True
字体(F) tsmiFont False
Text属性 Name属性 Checked属性
查看(V) tsmiView False
工具栏(T) tsmiToolStrip True
状态栏(S) tsmiStatusStrip True
Text属性 Name属性
帮助(H) tsmiHelp
关于记事本(A) tsmiAbout
(2)ToolStrip(工具栏控件)。
单击窗体左边会出现工具箱→ 找到“ToolStrip” →拖动到窗体中
单击工具栏→右下角属性→修改Name属性为“tlsNotepad”
右击工具栏→ 插入标准项
右击工具栏→ 编辑项 可以看到按钮成员 在这里我们可以删除或者增加,只设置几个简单的功能(新建,打开,保存,剪切,粘贴,复制)
(3)RichTextBox(多格式文本框控件)。
单击窗体左边会出现工具箱→ 找到“RichTextBox” →拖动到窗体中
单击工具栏→右下角属性→修改Name属性为“rtxtNotepad”
Anchor属性选择“Top,Bottom,Left,Right”,这样当窗体大小改变时,RichTextBox控件的大小也会跟着改变
(4)StatusStrip(状态栏控件)。
添加StatusStrip控件,将其Name属性设为“stsNotepad”,将Dock属性设为“Bottom”再将Anchor属性设为“Bottom,Left,Right”。然后单击 右边的 按钮,打开【项集合编辑器】对话框,下拉列表中保留默认的选择“StatusLabel”,然后单击【添加】按钮,依次添加2个StatusLabel,并分别命名为“tssLbl1”和“tssLbl2”,再将tssLbl1的Text属性设为“就绪”, tssLbl2的Text属性设为“显示日期、时间”
(5)OpenFileDialog(打开对话框)
从工具箱中找到OpenFileDialog直接拖入窗体中 单击属性
当用户单击记事本的【文件】→【打开】菜单项时,使用打开对话框OpenFileDialog打开文件。OpenFileDialog控件的Name属性为“odlgNotepad”,Filter属性设为“RTF文件|.rtf|所有文件|.*”
(6)SaveFileDialog(保存对话框)。
从工具箱中找到SaveFileDialog直接拖入窗体中 单击属性
当用户单击记事本的【文件】→【保存】(或【另存为】)菜单项时,使用保存对话框SaveFileDialog保存文件。SaveFileDialog控件的Name属性为“sdlgNotepad”,FileName属性改为“无标题”,Filter属性设为“RTF文件|*.rtf”
(7)FontDialog(字体对话框)
从工具箱中找到FontDialog直接拖入窗体中 单击属性
当用户单击记事本的【格式】→【字体】菜单项时,使用字体对话框FontDialog设置文本字体。FontDialog控件的Name属性为“fdlgNotepad”
(8) Timer(计时器控件)
从工具箱中找到Timer直接拖入窗体中 单击属性
Timer控件的Name属性设为“tmrNotepad”,Enabled(激活的可行的)属性设为“True”,Interval(间隔,间距,幕间时间)属性设为“1000”,表示1秒种触发一次Tick事件,即1秒钟改变一次时钟
二、源码展示
form1.cs
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;
namespace Editor
{
public partial class Form1 : Form
{
string filename = ""; //定义并初始化下文的文件名
public Form1()
{
InitializeComponent();
//初始化定时器
timer1.Enabled = true; //定时器的可见性
timer1.Interval = 1000; //定时器的时间间隔设置为1000ms
this.toolStripStatusLabel1.Text = "系统当前时间:" + DateTime.Now.ToString();
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
//点击此选项,关闭窗口并退出程序
this.Close();
Application.Exit();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
//新建选项
richTextBox1.Clear(); //清空丰富文本区
filename = ""; //开始的文件名为空
this.Text = "无标题-EditorPrimer"; //初始化文件标题
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
//打开文件选项,调用openFile对话框
openFileDialog1.Filter = "文本文件 | *.txt"; //设置文件类型过滤器
openFileDialog1.FilterIndex = 1; //设置文件对话框中当前选中文件筛选器的索引
openFileDialog1.InitialDirectory = "E:\\"; //设置文件对话框显示的初始目录
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//如果点击了确定按钮,更新文件名并向丰富区装载相应文本
filename = openFileDialog1.FileName;
//参数:需要装载的文件名以及装载的文本格式,这里指定纯文本
richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
this.Text = filename + "-EditorPrimer"; //更新窗口标题栏
}
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
//保存选项实现代码
if(filename.Length > 0) {
//文件名不是空,原来已经有此文件则直接保存
richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);
}
else
{
//文件名是空,属于新建的文件,调用另存为事件
toolStripMenuItem5_Click(sender, e);
}
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
//另存为选项,代码与打开文件选项类似
saveFileDialog1.Filter = "文本文件 | *.txt"; //设置保存文件类型过滤器
saveFileDialog1.FilterIndex = 1; //文件对话框中当前选定筛选器的索引
saveFileDialog1.InitialDirectory = "E:\\"; //保存文件时的默认目录
if(saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
//点击了保存文件对话框的确定按钮
filename = saveFileDialog1.FileName; //更新文件名
//保存文件,参数分别是文件的路径path 和文件的类型(这里指定为纯文本)
richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);
//获取文件保存路径并更新窗口标题栏
int index = filename.LastIndexOf('\\'); //找到最后一个斜线的下标索引
string Text_2 = filename.Substring(index + 1); //去掉文件路径获取文件名(参数下标索引,截取长度(不指定直到最后))
this.Text = Text_2 + "-EditorPrimer"; //更新标题栏
}
}
private void timer1_Tick(object sender, EventArgs e)
{
//每隔1000m更新一次时间数据达到动态电子表的效果
this.toolStripStatusLabel1.Text = "系统当前时间:" + DateTime.Now.ToString();
}
private void FontToolStripMenuItem_Click(object sender, EventArgs e)
{
fdlgNotepad.ShowColor = true;
if(fdlgNotepad.ShowDialog()==DialogResult.OK)
{
richTextBox1.SelectionColor = fdlgNotepad.Color;
richTextBox1.SelectionFont = fdlgNotepad.Font;
}
}
private void FindToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Owner = this;
form2.Show();
}
private void ReplaceToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Owner = this;
form3.Show();
}
}
}
form2.cs
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;
namespace Editor
{
public partial class Form2 : Form
{
public int start;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var MF = (Form1)this.Owner;
string str1; //存放要查找的文本
str1 = textBox1.Text; //获取要查找的文本
//查找下一个,查找区分大小写
start = MF.richTextBox1.Find(str1, start, RichTextBoxFinds.MatchCase);
if (start == -1) //如果返回值是-1,表示没有找到
{ //显示查找结束对消息框
MessageBox.Show("已查找到文档的结尾", "查找结束对话框");
start = 0; //查找位置赋值为0,从头开始查找
} else
//查找到
start = start + str1.Length;
//设置下一次查找的起始位置
MF.richTextBox1.Focus(); //为richTextBox1设置焦点
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
form3.cs
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;
namespace Editor
{
public partial class Form3 : Form
{
public int start;
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var MF = (Form1)this.Owner;
string str1, str2; //存放要查找的文本和要替换的文本
str1 = textBox1.Text; //获取要查找的文本
str2 = textBox2.Text; //获取要替换的文本
start = MF.richTextBox1.Find(str1, 0, RichTextBoxFinds.MatchCase);
//查找下一个
while (start != -1) //如果找到
{
MF.richTextBox1.SelectedText = str2; //替换
start = start + str2.Length; //下一次查找的起始位置
start = MF.richTextBox1.Find(str1, start, RichTextBoxFinds.MatchCase); //查找下一个
}
MessageBox.Show("已替换到文档的结尾", "查找结束对话框"); //显示查找结束对消息框
start = 0; //查找位置赋值为0,从头开始查找
MF.richTextBox1.Focus(); //为richTextBox1设置焦点
this.Close();
}
}
}
三、最终界面


浙公网安备 33010602011771号