一、开发环境及拓展资源
Visual Studio 2019
至少需安装 .NET桌面开发
二、创建窗体文件
1、创建窗体文件
2、配置项目名称及框架
3、设计界面
创建窗体文件,将控件摆放位置如下,参考系统自带的记事本程序
4、窗体添加的控件和组件如下
控件及组件在工具箱查找
5、窗体属性
6、快捷键设置
7、程序属性
- 项目属性如下图,在创建项目时就已定好了框架,如果在另一台主机上的框架版本比目前项目框架版本低的话,则运行不起来
文章末尾有整个程序的压缩包链接可下载,如需直接运行则需下载对应的.NET Framework 4.7.2框架
- 程序图标可在此设置,生成程序后的图标如下图,此文件夹下的程序文件可在第二台主机上直接运行(项目\bin\Debug目录下就是生成程序文件的存放位置,双击程序文件即可运行)
三、代码演示
代码开头的using部分,注释部分需自行添加
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;//提供了关于文件、数据流的读取和写入操作
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;//提供了用于与事件日志、性能计数器和系统进程进行交互的类
主要功能
- 新建文件:
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtBox.Modified == true)
{
DialogResult dr = MessageBox.Show("文件发生变化,是否更改保存?", "注意", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
保存SToolStripMenuItem_Click(sender, e);
return;
}
else if (dr == DialogResult.Cancel)
{
return;
}
txtBox.Clear();
this.Text = "NewNotepad";
}
else
{
txtBox.Clear();
this.Text = "NewNotepad";
}
}
- 打开:
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog.FileName;
OpenFile();
}
}
protected void OpenFile()
{
try
{
txtBox.Clear();
txtBox.Text = File.ReadAllText(filename);
}
catch
{
MessageBox.Show("Error!"); }
}
- 保存:
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StreamWriter sw = File.AppendText(Application.ExecutablePath);
sw.Write(txtBox.Text);
sw.Dispose();
}
catch
{
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = "*.txt";
sf.Filter = "文本文档(.txt)|*.txt";
if (sf.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.AppendText(sf.FileName);
sw.Write(txtBox.Text);
sw.Dispose();
}
}
}
- 另存为:
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
string name;
//SaveFileDialog类
SaveFileDialog save = new SaveFileDialog();
//过滤器
save.Filter = "*.txt|*.TXT|(*.*)|*.*";
//显示
if (save.ShowDialog() == DialogResult.OK)
{
name = save.FileName;
FileInfo info = new FileInfo(name);
//info.Delete();
StreamWriter writer = info.CreateText();
writer.Write(txtBox.Text);
writer.Close();
}
}
- 打印:
private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
//显示允许用户选择打印机的选项及其它打印选项的对话框
this.printDialog.Document = this.printDocument;
this.printDialog.PrinterSettings = this.pageSetupDialog.PrinterSettings;
//向打印机发送打印指令
if (this.printDialog.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误信息!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
- 编辑:
根据输入是否输入内容控制是否启用功能
private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{
剪切ToolStripMenuItem.Enabled = txtBox.Modified;
if (txtBox.SelectedText == "")
{
剪切ToolStripMenuItem.Enabled = false;
复制ToolStripMenuItem.Enabled = false;
删除ToolStripMenuItem.Enabled = false;
}
else
{
剪切ToolStripMenuItem.Enabled = true;
复制ToolStripMenuItem.Enabled = true;
删除ToolStripMenuItem.Enabled = true;
}
if (txtBox.Text == "")
{
查找ToolStripMenuItem.Enabled = false;
查找下一个ToolStripMenuItem.Enabled = false;
查找上一个ToolStripMenuItem.Enabled = false;
替换ToolStripMenuItem.Enabled = false;
}
else
{
查找ToolStripMenuItem.Enabled = true;
查找下一个ToolStripMenuItem.Enabled = true;
查找上一个ToolStripMenuItem.Enabled = true;
替换ToolStripMenuItem.Enabled = true;
}
if (Clipboard.GetText() == "")
粘贴ToolStripMenuItem.Enabled = false;
else
粘贴ToolStripMenuItem.Enabled = true;
}
7.查找:(查找功能不够完善,混用查找上一项和查找下一项效果不理想)
TextBox txtInput = new TextBox()
{
Font = new Font("宋体", 10)
};
TextBox txtInputReplace = new TextBox()
{
Font = new Font("宋体", 10)
};
Label lblSearch = new Label
{
Text = "查找内容:",
Size = new Size(65, 25),
Location = new Point(5, 22)
};
Label lblDirection = new Label
{
Text = "查找方向:",
Size = new Size(65, 25),
Location = new Point(5, 58)
};
Button FindNext = new Button
{
Name = "btnFindNext",
Text = "查找下一项",
Size = new Size(80, 25),
Location = new Point(265, 15)
};
Button Cancel = new Button
{
Name = "btnCancel",
Text = "取消",
Size = new Size(80, 25),
Location = new Point(265, 50)
};
RadioButton down = new RadioButton
{
Name = "radDown",
Text = "向下",
Size = new Size(55, 25),
Location = new Point(70, 53),
Checked = true
};
RadioButton upward = new RadioButton
{
Name = "radUpward",
Text = "向上",
Size = new Size(55, 25),
Location = new Point(140, 53),
Checked = false
};
new Form FindForm = new Form
{
Text = "查找文本",
FormBorderStyle = FormBorderStyle.FixedSingle,
MaximizeBox = false,
MinimizeBox = false
};
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
//显示查找对话框
txtInput.Size = new Size(190, 33);
txtInput.Location = new Point(70, 15);
txtInput.Multiline = true;
FindNext.Click += new EventHandler(Direction_Click);
//FindNext.Click += new EventHandler(Visible_Click);
Cancel.Click += new EventHandler(Cancel_Click);
FindForm.Controls.Add(lblSearch);
FindForm.Controls.Add(lblDirection);
FindForm.Controls