记事本 源码工程下载(需要1资源分)。
拖放控件: menuStrip toolStrip fontDialog colorDialog contextMenuStrip statusStrip timer (两个)
《拓展功能》中的toolStripComboBox1.Items(集合)自己定义(就是上图显示下拉框),用于自动保存的时间。
/// --自动保存--
/// 用户自定义 声明的--自动保存--方法
///
///
///
private void AuSaveD()
{
int sj;
string str1 = toolStripComboBox1.Text;
sj = Convert.ToInt32(str1);
//sj = int.Parse(str1);
if (自动保存SToolStripMenuItem.Checked == true)
{
自动保存SToolStripMenuItem.Checked = true;
保存模式toStabel.Text = "自动模式:";
保存模式toStabel.ForeColor = Color.Purple; //紫色
timer2.Interval = sj * 60000;
timer2.Enabled = true;
}
else
{
自动保存SToolStripMenuItem.Checked = false; timer2.Enabled = false;
保存模式toStabel.Text = "手动模式:";
保存模式toStabel.ForeColor = Color.Black; //黑色
}
}
timer2.Interval = sj * 60000; // 定义toolStripComboBox1.Text为多少分钟自动保存
toolStripComboBox1.SelectedIndex = 2; // 在装载窗体中设置保存时间默认项
contextMenuStrip1.Name=cotxtMeStr; 用于文本右击菜单。(详细功能自己设置)
toolStrip1(工具栏)插入标准项(如下图),自定义项要自己选择图片。然后 编辑项 的顺序,分组(如上图显示)。
记事本主窗口代码:RichTextBox1.Name=txtInput;
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;
//using System.Reflection;
namespace Notepad
{
public partial class Form1 : Form
{
public Form1()
{ //这里面尽可能不要放代码
InitializeComponent();
}
#region - 获取光标坐标位置 直接使用方法 Ranks();
/// 自定义方法 --
/// 获取文本中(行和列)--光标--坐标位置的调用方法
///
private void Ranks()
{
/* 得到光标行第一个字符的索引,
* 即从第1个字符开始到光标行的第1个字符索引*/
int index = txtInput.GetFirstCharIndexOfCurrentLine();
/*得到光标行的行号,第1行从0开始计算,习惯上我们是从1开始计算,所以+1。 */
int line = txtInput.GetLineFromCharIndex(index) + 1;
/* SelectionStart得到光标所在位置的索引
* 再减去
* 当前行第一个字符的索引
* = 光标所在的列数(从0开始) */
int col = txtInput.SelectionStart - index + 1;
this.行列toStabel.Text = string.Format("第:{0}行 {1}列", line.ToString(), col.ToString());
}
//释放按键时发生
private void txtInput_KeyUp(object sender, KeyEventArgs e)
{
this.Ranks();
}
//释放鼠标时发生
private void txtInput_MouseUp(object sender, MouseEventArgs e)
{
this.Ranks();
}
#endregion
#region --自定义的变量--
/// -- txtName 用于储存文件名 --
private string txtName = "";
private string filePath = "";//储存文件路径
/// -- b 判断是否为打开的文件,true为真 --
bool b = false;
/// -- s 判断文本是否未保存,true为保存 --
bool s = true;
#endregion
#region -- 菜单中的 -- 新建/保存/另存为--等声明方法
/// -- 新建文本 --
/// 用户自定义 声明的--新建--方法
///
private void Newfile()
{ /* 新建文本,保存后清空text,s 的值会变为false,所以要把 s =true */
if (b == true)//判断是否为打开的文件
{
this.AskDialog();
}
else
{
this.AskDialog();
}
Text = "新建-Mxdr记事本";
//清除储存的文件名
txtName = "";
//新建文本文档后应该把 b 设为 false
b = false;
}
/// --打开--
/// 用户自定义 声明的--打开--方法
///
///
///
private void Openthe()
{
if (s == true)
{
this.OpenFile();
}
else
{
this.AskDialog();
this.OpenFile();
}
}
/// --打开文件的方法--
/// 用户自定义
///
///
///
private void OpenFile()
{
OpenFileDialog ofd = new OpenFileDialog();
//对话框提示标题文字。
ofd.Title = "打开文本文档-Mxdr记事本";
//过滤文件,只显示文本文档格式的文件。
ofd.Filter = "文本文档|*.txt|所有文档|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
//声明一个文件流对象,用来存放读取的文件流对象。
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.None);
//将文件名赋值给临时变量。
txtName = ofd.FileName;
//创建读取器,用来将文件流中的数据读取出来。
StreamReader sr = new StreamReader(fs, Encoding.Default);
//通过读取器将文件流中的数据读取到文本框中。
txtInput.Text = sr.ReadToEnd();
//关闭读取器。
sr.Close();
//和关闭文件流。
fs.Close();
//由于文件读取原理,上面两项关闭必须操作,否则无法成功读取文件。
}
this.Text = txtName + "-Mxdr记事本";
b = true;
s = true;
}
/// --保存--
/// 用户自定义 声明的--保存--方法
///
///
///
protected void Save()
{
if (b == true)
{
SaveFileDialog ofg = new SaveFileDialog();
txtInput.SaveFile(txtName, RichTextBoxStreamType.PlainText);
s = true;
}
else { SaveAs(); }
}
/// --另存为--
/// 用户自定义 声明的--另存为--方法
///
///
///
private void SaveAs()
{
SaveFileDialog ofd = new SaveFileDialog();
//对话框提示标题文字。
ofd.Title = "另存为-Mxdr记事本";
//保存文本文档,以.txt作为后缀名,如果有则不加,否则添加后缀.txt。
ofd.Filter = "文本文档|*.txt|所有文档|*.*";
ofd.FileName = "Mxdr记事本-" + DateTime.Now.ToString("yyyyMMddHHmm")+".txt"; //保存文件名
if (ofd.ShowDialog() == DialogResult.OK)
{
txtInput.SaveFile(ofd.FileName, RichTextBoxStreamType.PlainText);
txtName = "";
filePath = "";
Text = "新建-Mxdr记事本";
this.txtInput.Text = "";
s = true;
}
}
/// --询问对话框--
/// 用户自定义 询问用户是否保存已改变的文本内容
///
///
///
private void AskDialog()
{
//如果文本未保存,提示保存
if (s == false) //(this.richTextBox1.Text != string.Empty)
{
DialogResult r = MessageBox.Show("是否保存当前文件?",
"Mxdr温馨提示",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information);
/* 判断用户选择,若选择"是/Yes",弹出保存文件的对话框
* 若选择"否/No",不弹对话框,直接清空内容
* 若选择"取消/Cancel",无动作 */
if (r == DialogResult.Yes)
{
// 保存文件
Save();
//txtName = "";
//filePath = "";
//Text = "新建-Mxdr记事本";
//this.txtInput.Text = "";
////OpenFile();
//s = true;
}
else if (r == DialogResult.No)
{
// 用户选择不保存时将输入框中的内容清除
//this.richTextBox1.Text = "";
Text = "新建-Mxdr记事本";
this.txtInput.Clear();
//如果文本为 Null 把统计还原
if (txtInput.Text.Length <= 0)
{
大小toStabel.Text = "大小:0KB";
计数toStabel.Text = "其中:0/符号,0/字母,0/数字,0/汉字 =0字";
}
this.Ranks();
b = false;
s = true;
}
}
else //否则直接清空文本
{
//this.txtInput.Clear(); //清除文本内容
this.txtInput.Text = "";
//如果文本为 Null 把统计还原
if (txtInput.Text.Length <= 0)
{
大小toStabel.Text = "大小:0KB";
计数toStabel.Text = "其中:0/符号,0/字母,0/数字,0/汉字 =0字";
}
this.Ranks();
s = true;
}
}
#endregion
#region -- 文件菜单中的 -- 新建 - 打开 -- 保存 - 另存为 --
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
Newfile();
}
private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
Newfile();
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
Openthe();
}
private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
Openthe();
}
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
Save();
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
Save();
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAs();
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region -- 菜单中的 -- 剪切/复制/粘贴/撤消/删除--查找/替换--
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Undo();
}
private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Cut();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Copy();
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Paste();
}
private void 剪切UToolStripButton_Click(object sender, EventArgs e)
{
this.txtInput.Cut();
}
private void 复制CToolStripButton_Click(object sender, EventArgs e)
{
this.txtInput.Copy();
}
private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
{
this.txtInput.Paste();
}
//删除
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{//删除
this.txtInput.Text = txtInput.Text.Remove(txtInput.SelectionStart,
txtInput.SelectionLength);
}
//查找
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
formFind findform = new formFind();
findform.Show(this.txtInput);
}
//查找下一个
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
formFind findform = new formFind();
findform.Show(this.txtInput);
}
//替换
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
formFind findform = new formFind();
findform.Show(this.txtInput);
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.SelectAll();
}
private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Text = txtInput.Text.Insert(txtInput.Text.Length,
"当前时间:" + System.DateTime.Now.ToString());
txtInput.SelectionStart = txtInput.Text.Length;
txtInput.Focus();//获得焦点
this.txtInput.ScrollToCaret();
}
#endregion --
#region ---- 菜单中的 -- 自动换行/字体/背景
private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动换行WToolStripMenuItem.Checked != true)
{
//自动换行点击Checked属性更改,多文本WordWrap为false
自动换行WToolStripMenuItem.Checked = false;
txtInput.WordWrap = false;
}
else
{
自动换行WToolStripMenuItem.Checked = true;
txtInput.WordWrap = true;
}
}
private void 字体FToolStripMenuItem1_Click(object sender, EventArgs e)
{
FontDialog font = new FontDialog();
if (font.ShowDialog() == DialogResult.OK)
{ this.txtInput.Font = font.Font; }
}
private void 字体颜色bToolStripMenuItem_Click(object sender, EventArgs e)
{
//创建颜色对话框
colorDialog1.ShowDialog();
txtInput.SelectionColor = colorDialog1.Color;
}
private void 默认字体颜色CToolStripMenuItem_Click(object sender, EventArgs e)
{
txtInput.SelectionColor = Color.Black;
}
private void 文本背景ToolStripMenuItem_Click(object sender, EventArgs e)
{
//创建颜色对话框
colorDialog1.ShowDialog();
txtInput.BackColor = colorDialog1.Color;
文本背景ToolStripMenuItem.Checked = true;
默认背景RToolStripMenuItem.Checked = false;
}
private void 默认背景RToolStripMenuItem_Click(object sender, EventArgs e)
{
if (默认背景RToolStripMenuItem.Checked != false)
{
默认背景RToolStripMenuItem.Checked = true;
文本背景ToolStripMenuItem.Checked = false;
txtInput.BackColor = Color.WhiteSmoke;// WhiteSmoke;
}
}
#endregion --
#region -- 菜单中的 -- 工具栏/状态栏
//选项与自定义未设置
private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 工具栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
Point point;
if (工具栏ToolStripMenuItem.Checked != true)
{
// 隐藏工具栏时,把坐标设为(0,24),因为菜单的高度为24
point = new Point(0, 24);
工具栏ToolStripMenuItem.Checked = false;
toolStrip1.Visible = false;
txtInput.Location = point;
txtInput.Height += toolStrip1.Height;
}
else
{
/* 显示工具栏时,多格式文本框左上角位置的位置为(0,49),
* 因为工具栏的高度为25,加上菜单的高度24后为49
*/
point = new Point(0, 49);
工具栏ToolStripMenuItem.Checked = true;
toolStrip1.Visible = true;
txtInput.Location = point;
txtInput.Height -= toolStrip1.Height;
}
}
private void 状态栏SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (状态栏SToolStripMenuItem.Checked == false)
{
//状态栏SToolStripMenuItem.Checked = false;
statusStrip1.Visible = false;
txtInput.Height += statusStrip1.Height;
}
else
{
//状态栏SToolStripMenuItem.Checked = true;
statusStrip1.Visible = true;
txtInput.Height -= statusStrip1.Height;
}
}
#endregion --
#region -- 菜单中的 -- 对齐方式
private void 左对齐ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*利用richTextBox1的属性SelectionAlignment
* 获取或设置应用到当前选定内容或插入点的对齐方式。
* 利用HorizontalAlignment 枚举的成员Left,
* 设置对象或文本与控件元素的左侧对齐。
*/
if (左对齐ToolStripMenuItem.Checked == true)
{
this.txtInput.SelectionAlignment = HorizontalAlignment.Left;
左对齐ToolStripMenuItem.Checked = true;
居中ToolStripMenuItem.Checked = false;
右对齐ToolStripMenuItem.Checked = false;
}
}
private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*利用richTextBox1的属性SelectionAlignment
* 获取或设置应用到当前选定内容或插入点的对齐方式。
* 利用HorizontalAlignment 枚举的成员Center,
* 设置对象或文本与控件元素的左侧对齐。
*/
if (居中ToolStripMenuItem.Checked == true)
{
this.txtInput.SelectionAlignment = HorizontalAlignment.Center;
左对齐ToolStripMenuItem.Checked = false;
居中ToolStripMenuItem.Checked = true;
右对齐ToolStripMenuItem.Checked = false;
}
}
private void 右对齐ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*利用richTextBox1的属性SelectionAlignment
* 获取或设置应用到当前选定内容或插入点的对齐方式。
* 利用HorizontalAlignment 枚举的成员Right,
* 设置对象或文本与控件元素的左侧对齐。
*/
if (右对齐ToolStripMenuItem.Checked == true)
{
this.txtInput.SelectionAlignment = HorizontalAlignment.Right;
左对齐ToolStripMenuItem.Checked = false;
居中ToolStripMenuItem.Checked = false;
右对齐ToolStripMenuItem.Checked = true;
}
}
#endregion --
#region -- 菜单中的 -- 回车多行/自动保存/显示时间
private void 回车多行EToolStripMenuItem_Click(object sender, EventArgs e)
{
if (回车多行EToolStripMenuItem.Checked != true)
{
StripMenuEnt.Text = "回车单行";
StripMenuEnt.ForeColor = Color.Blue;
回车多行EToolStripMenuItem.Checked = false;
}
else
{
回车多行EToolStripMenuItem.Checked = true;
StripMenuEnt.Text = "回车多行";
StripMenuEnt.ForeColor = Color.Purple;
}
}
//回车多行
private void EntBtton_Click(object sender, EventArgs e)
{
if (回车多行EToolStripMenuItem.Checked == true)
{ 回车多行EToolStripMenuItem.Checked = false;
StripMenuEnt.Text = "回车单行";
StripMenuEnt.ForeColor = Color.Blue;
}
else
{ 回车多行EToolStripMenuItem.Checked = true;
StripMenuEnt.Text = "回车多行";
StripMenuEnt.ForeColor = Color.Purple;
}
}
private void 自动保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
AuSaveD();
}
/// --自动保存--
/// 用户自定义 声明的--自动保存--方法
///
///
///
private void AuSaveD()
{
int sj;
string str1 = toolStripComboBox1.Text;
sj = Convert.ToInt32(str1);
//sj = int.Parse(str1);
if (自动保存SToolStripMenuItem.Checked == true)
{
自动保存SToolStripMenuItem.Checked = true;
保存模式toStabel.Text = "自动模式:";
保存模式toStabel.ForeColor = Color.Purple; //紫色
timer2.Interval = sj * 60000;
timer2.Enabled = true;
}
else
{
自动保存SToolStripMenuItem.Checked = false; timer2.Enabled = false;
保存模式toStabel.Text = "手动模式:";
保存模式toStabel.ForeColor = Color.Black; //黑色
}
}
// 自动保存
private void SaveButton_Click(object sender, EventArgs e)
{
if (自动保存SToolStripMenuItem.Checked == false)
{
自动保存SToolStripMenuItem.Checked = true;
AuSaveD();
}
else
{
自动保存SToolStripMenuItem.Checked = false;
保存模式toStabel.Text = "手动模式:";
保存模式toStabel.ForeColor = Color.Black; //黑色
timer2.Enabled = false;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (自动保存SToolStripMenuItem.Checked == true && b == true)
{
Save();
}
}
private void 显示时间DToolStripMenuItem_Click(object sender, EventArgs e)
{
if (显示时间DToolStripMenuItem.Checked != true)
{ 时间toStrMenItem.Text = "时间:"; }
//else { 显示时间DToolStripMenuItem.Checked = true; }
}
// 工具栏点击显示时间
private void SjDataButton_Click(object sender, EventArgs e)
{
if (显示时间DToolStripMenuItem.Checked == false)
{ 显示时间DToolStripMenuItem.Checked = true; }
else { 显示时间DToolStripMenuItem.Checked = false;
时间toStrMenItem.Text = "时间:";
}
}
#endregion --
#region -- 菜单中的 -- 帮助菜单
private void 内容CToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 帮助LToolStripButton_Click(object sender, EventArgs e)
{
}
#endregion --
// 保存状态
private void timer1_Tick(object sender, EventArgs e)
{
if (显示时间DToolStripMenuItem.Checked == true)
{
时间toStrMenItem.Text = "时间:" + System.DateTime.Now.ToString();
}
if (s == true)//状态栏中的判断
{
保存toStabel.Text = "已保存";
保存toStabel.ForeColor = Color.Black;
}
else
{ 保存toStabel.Text = "未保存"; 保存toStabel.ForeColor = Color.Purple; }
}
#region --为其他 Form 打开关联 --
/// 关联 formFind.cs
/// 关联 formFind.cs
///
///
///
public RichTextBox RichTxtBox
{
get
{ return txtInput; }
set
{ txtInput = value; }
}
#endregion
//退出前询问
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (s == false) //判断文件是否为未保存
{
if (e.CloseReason == CloseReason.UserClosing)
{
DialogResult r = MessageBox.Show(
"文件内容已发生改变,\n确定要退出程序?",
"温馨提示 - Mxdr记事本",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if (r != DialogResult.OK)
{
e.Cancel = true; //如果对话框点击的不是"OK",取消本次退出
}
}
}
}
//按下并释放键时发生--回车事件--
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
if (回车多行EToolStripMenuItem.Checked == true)
{
if (e.KeyChar == (char)13)
{
string s = txtInput.Text; //.Text;//实例化
int ls = txtInput.SelectionStart;//获得文本框中光标位置
s = s.Insert(ls, "\r\n ");//在光标后面要插入字符串
txtInput.Text = s;//在光标后面插入字符串
//插入字符串后,光标要放在后面还是前面(+光标放在后面,数字要把光标移动的位置)
txtInput.Focus();//获得焦点
txtInput.SelectionStart = ls + 5;
this.txtInput.ScrollToCaret();
}
}
}
//窗体装载
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;//开启计时器
Point point;
point = new Point(0, 24);
工具栏ToolStripMenuItem.Checked = false;
toolStrip1.Visible = false;
txtInput.Location = point;
txtInput.Height += toolStrip1.Height;
toolStripComboBox1.SelectedIndex = 2;//设置保存时间默认项
}
//文本框改变后发生
private void txtInput_TextChanged(object sender, EventArgs e)
{
this.calculate();
}
#region 自定义函数 获得汉字、字母、数字、符号
/// 统计文本中的 -- 数字、字母、汉字、符号 --
///
///
///
private void calculate()
{
bool skipSpace = true;
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
int cnt5, cnt6;
if (txtInput.Text.Length <= 0) { s = true; }
else
if (this.txtInput.Text != null)
{
string sLine;
string ls = txtInput.Text;
if (skipSpace)
{
sLine = ls.Replace(" ?", "").Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
cnt2 += getByteLength(sLine); //字节数量(不含" ","\t","\n","\r"," ?")
cnt1 += getWordLength(sLine); //字数
cnt3 += getdigitalLength(sLine); //数字数量
cnt4 += getcharLength(sLine); //字母数量
cnt5 = cnt2 - cnt1; //汉字数量
cnt6 = cnt2 - cnt3 - cnt4 - cnt5 * 2; //符号数量
//读取字符数
大小toStabel.Text = "大小:" + ((txtInput.Text.Length + (cnt2 - cnt1))).ToString() + "KB";
计数toStabel.Text = "其中:" + cnt6.ToString() + "/符号," +
cnt4.ToString() + "/字母," + cnt3.ToString() + "/数字," +
cnt5.ToString() + "/汉字 =" + cnt1.ToString() + "字";
}
s = false;
}
}
/// 返回字数
///
///
///
private int getWordLength(string s)
{
if (s != null)
return s.Length;
else
return 0;
}
/// 返回数字(0~9)字数数量
///
///
///
private int getdigitalLength(string s)
{
int lx = 0;
char[] q = s.ToCharArray();
for (int i = 0; i < q.Length; i++)
{
if ((int)q[i] >= 48 && (int)q[i] <= 57)
{
lx += 1;
}
}
return lx;
}
/// 返回字母(A~Z-a~z)字数数量
///
///
///
private int getcharLength(string s)
{
int lz = 0;
char[] q = s.ToLower().ToCharArray();
for (int i = 0; i < q.Length; i++)
{
if ((int)q[i] >= 97 && (int)q[i] <= 122)
{
lz += 1;
}
}
return lz;
}
/// 返回字节数
///
///
///
private int getByteLength(string s)
{
int lh = 0;
char[] q = s.ToCharArray();
for (int i = 0; i < q.Length; i++)
{
if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5) // 汉字
{
lh += 2;
}
else
{
lh += 1;
}
}
return lh;
}
#endregion
#region //--richTextBox1 文本中的右键菜单--剪切-复制-粘贴-关闭窗口--
private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Cut();
}
private void 复制CToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.txtInput.Copy();
}
private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtInput.Paste();
}
private void 撤消toolStripMenuItem5_Click_1(object sender, EventArgs e)
{
this.txtInput.Undo();
}
private void 全选ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
this.txtInput.SelectAll();
}
private void 删除toolStripMenuItem7_Click(object sender, EventArgs e)
{
this.txtInput.Text = txtInput.Text.Remove(txtInput.SelectionStart,
txtInput.SelectionLength);
}
private void 关闭窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
}
}
查找-替换窗体:如下图所示,除了TextBox1、区分大小写、开启替换、方向和退出 启用外,其他都禁用。
TextBox1输入文本后,查找按键启用,如果要替换,勾选《开启替换》。
formFind.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 Notepad
{
public partial class formFind : Form
{
public formFind()
{
InitializeComponent();
}
private void formFind_Load(object sender, EventArgs e)
{
text = ((Form1)this.Owner).RichTxtBox.Text;
inout2 = text.Length;
radioButton2.Checked = true;
}
string text = string.Empty;
string f1 = string.Empty;
public int a, b;
public int inout1 = 0;
public int inout2 = 0; //inout2 要放在Load赋值
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
textBox2.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == false) //是否区分大小写
{
MatchSize();
}
if (radioButton2.Checked == true)
{
DownSeek();
}
if (radioButton1.Checked == true)//向上查找
{
UpSeek();
}
}
private void button2_Click(object sender, EventArgs e)
{
replace();
}
private void button3_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == false) //是否区分大小写
{
MatchSize();
}
//if (radioButton2.Checked == true) //向下查找
//{
//Form1 f1 = (Form1)this.Owner;
//string f1_text = f1.RichTextBox1.Text;
inout1 = 0; a = 0;
a = text.IndexOf(textBox1.Text, inout1);
for (int i = 0; i < a; i++)
{
DownSeek();
replace();
}
}
//关闭窗口
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
//区分大小写
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
MatchSize();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = true;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
button2.Enabled = true; button3.Enabled = true;
}
#region 自定义的--查找----替换--
/// 区分大小写
/// 转换为小写定义
///
///
///
private void MatchSize()
{
Form1 f1 = (Form1)this.Owner;
string f1_text = f1.RichTxtBox.Text;
f1_text = f1_text.ToLower();//文本转换为小写
textBox1.Text = textBox1.Text.ToLower(); //查找内容转换小写
//textBox2.Text = textBox2.Text.ToLower(); //替换字符转换为小写
}
/// -向下查找-
/// 自定义向下查找字符串 -向下查找-
///
///
///
private void DownSeek()
{
b = text.Length;
inout2 = text.Length;
Form1 f1 = (Form1)this.Owner;
string f1_text = f1.RichTxtBox.Text;
//inout1 = 0;
a = f1_text.IndexOf(textBox1.Text, inout1); //向下查找
if (a >= 0)
{
f1.RichTxtBox.Select(a, textBox1.Text.Length);
f1.RichTxtBox.Focus();
inout1 = ++a;
}
else
{
MessageBox.Show(this.Owner,
"查找不到" + "\"" + textBox1.Text + "\"",
"-Mxdr记事本",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
//return;
a = 0;
inout1 = 0;
}
}
/// --向上查找--
/// 自定义向上查找字符串 --向上查找--
///
///
///
private void UpSeek()
{
a = 0;
inout1 = 0;
Form1 f1 = (Form1)this.Owner;
string f1_text = f1.RichTxtBox.Text;
//inout2 = 0;
b = f1_text.LastIndexOf(textBox1.Text, inout2);
if (b >= 0)
{
f1.RichTxtBox.Select(b, textBox1.Text.Length);
f1.RichTxtBox.Focus();
inout2 = --b;
}
else
{
MessageBox.Show(this.Owner,
"查找不到" + "\"" + textBox1.Text + "\"",
"-Mxdr记事本",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
//return;
b = text.Length;
inout2 = text.Length;
}
}
/// --替换--自定义
/// --替换-- 自定义替换字符串
///
///
///
private void replace()
{
Form1 f1 = (Form1)this.Owner;
if (f1.RichTxtBox.SelectedText.Length > 0)
f1.RichTxtBox.SelectedText = this.textBox2.Text;//textBox2中放要替换的字符
}
#endregion
}
}
希望这对你有用!!!