目录
我的代码由三个窗体,Form1为记事本窗体,还有一部分代码就不需要看了,应为都是系统自动生成的代码
下面是记事本的Form1事件里面的内容 。
下面是Form2事件的内容,这个是替换的对话框
下面是Form3 的事件内容,为查找框里面的内容
我的代码由三个窗体,Form1为记事本窗体,还有一部分代码就不需要看了,应为都是系统自动生成的代码
下面是记事本的Form1事件里面的内容 。
下面是代码,里面有剪切,粘贴还有其他功能。
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.IO;
namespace 记事本开发1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String Filename=String.Empty;
public int guangbiao;
public String str;
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.WordWrap == true)
{
textBox1.WordWrap = false;
}
else
{
textBox1.WordWrap = true;
}
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
StreamWriter myStream;
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string str;
str = saveFileDialog1.FileName;
myStream = new StreamWriter(saveFileDialog1.FileName);
myStream.Write(textBox1.Text);
myStream.Flush();
myStream.Close();
}
}
private void 打开OCtrlOToolStripMenuItem_Click(object sender, EventArgs e)
{
string resultFile;
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
resultFile = openFileDialog1.FileName;
this.Text = openFileDialog1.SafeFileName + "-记事本";
StreamReader reader = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default);
textBox1.Text = reader.ReadToEnd();
this.Filename = openFileDialog1.FileName;
reader.Close();
}
}
private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
}
}
private void 撤销UCtrlZToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.CanUndo == true)
{
textBox1.Undo();
textBox1.ClearUndo();
}
}
private void 复制CCtrlCToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.SelectionLength != 0)
{
textBox1.Copy();
}
}
private void 剪切TCtrlXToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.SelectionLength != 0)
{
textBox1.Cut();
}
}
private void 全选ACtrlAToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.Text != string.Empty)
{
textBox1.SelectAll();
}
}
private void 新建CtrlNToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.Modified == true)
{
DialogResult result = MessageBox.Show("文本发生了改变,要保存吗?", "注意", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
if (this.Filename == String.Empty)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
DialogResult bresult = saveFileDialog1.ShowDialog();
if (bresult == DialogResult.Cancel)
{
textBox1.Text = textBox1.Text;
}
else
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.ShowDialog();
StreamWriter writer = new StreamWriter(saveFileDialog1.FileName, true, ASCIIEncoding.Default);
writer.Write(textBox1.Text);
writer.Close();
textBox1.Modified = false;
}
}
else
{
StreamWriter writer = new StreamWriter(this.Filename, true, ASCIIEncoding.Default);
writer.Write(textBox1.Text);
writer.Close();
textBox1.Modified = false;
}
}
else if (result == DialogResult.No)
{
textBox1.Clear();
}
else
{
textBox1.Text = textBox1.Text;
}
}
else
{
textBox1.Clear();
}
}
private void 保存SCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.Filename == string.Empty)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.Cancel)
{
textBox1.Text = textBox1.Text;
}
else
{
StreamWriter writer = new StreamWriter(saveFileDialog1.FileName,true,ASCIIEncoding.Default);
writer.Write(textBox1.Text);
writer.Close();
textBox1.Modified = false;
}
}
else
{
StreamWriter writer = new StreamWriter(this.Filename, true, ASCIIEncoding.Default);
writer.Write(textBox1.Text);
writer.Close();
textBox1.Modified = false;
}
}
private void 粘贴PCtrlVToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Clipboard.GetText().ToString() != "")
{
textBox1.Paste();
}
}
private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.SelectedText != string.Empty)
{
textBox1.SelectedText = string.Empty;
}
}
private void 时间日期DF5ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + DateTime.Now.ToString();
}
private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.Modified == false)
{
撤销UCtrlZToolStripMenuItem.Enabled = false;
}
else
{
撤销UCtrlZToolStripMenuItem.Enabled = true;
}
if (textBox1.SelectedText == "")
{
剪切TCtrlXToolStripMenuItem.Enabled = false;
删除LToolStripMenuItem.Enabled = false;
复制CCtrlCToolStripMenuItem.Enabled = false;
}
else
{
剪切TCtrlXToolStripMenuItem.Enabled = true;
删除LToolStripMenuItem.Enabled = true;
复制CCtrlCToolStripMenuItem.Enabled = true;
}
if (textBox1.Text == "")
{
查找FCtrlFToolStripMenuItem.Enabled = false;
替换RCtrlHToolStripMenuItem.Enabled = false;
}
else
{
查找FCtrlFToolStripMenuItem.Enabled = true;
替换RCtrlHToolStripMenuItem.Enabled = true;
}
if (Clipboard.GetText().ToString() == "")
{
粘贴PCtrlVToolStripMenuItem.Enabled = false;
}
else
{
粘贴PCtrlVToolStripMenuItem.Enabled = true;
}
}
private void 查找FCtrlFToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 cc = new Form3();
cc.Owner = this;
guangbiao = textBox1.SelectionStart;
str = textBox1.Text;
cc.Show();
}
private void 替换RCtrlHToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dd = new Form2();
dd.Owner = this;
guangbiao = textBox1.SelectionStart;
str = textBox1.Text;
dd.Show();
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
guangbiao = textBox1.SelectionStart;
}
private void 查找下一项NF3ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 cc = new Form3();
cc.Owner = this;
guangbiao = textBox1.SelectionStart;
str = textBox1.Text;
cc.Show();
}
}
}
namespace 记事本开发1._0
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.文件FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.新建CtrlNToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.打开OCtrlOToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.保存SCtrlSToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.另存为AToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
this.页面设置UToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.打印ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.编辑EToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.撤销UCtrlZToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.剪切TCtrlXToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.复制CCtrlCToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.粘贴PCtrlVToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.删除LToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.查找FCtrlFToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.查找下一项NF3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.替换RCtrlHToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.转到GCtrlGToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.全选ACtrlAToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.时间日期DF5ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.格式OToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.自动换行ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.字体FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.查看VToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.状态栏SToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.帮助ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.查看帮助HToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
this.关于记事本AToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.textBox1 = new System.Windows.Forms.TextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.fontDialog1 = new System.Windows.Forms.FontDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.文件FToolStripMenuItem,
this.编辑EToolStripMenuItem,
this.格式OToolStripMenuItem,
this.查看VToolStripMenuItem,
this.帮助ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1043, 25);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// 文件FToolStripMenuItem
//
this.文件FToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.新建CtrlNToolStripMenuItem,
this.打开OCtrlOToolStripMenuItem,
this.保存SCtrlSToolStripMenuItem,
this.另存为AToolStripMenuItem,
this.toolStripSeparator4,
this.页面设置UToolStripMenuItem,
this.打印ToolStripMenuItem,
this.toolStripSeparator5,
this.退出ToolStripMenuItem});
this.文件FToolStripMenuItem.Name = "文件FToolStripMenuItem";
this.文件FToolStripMenuItem.Size = new System.Drawing.Size(58, 21);
this.文件FToolStripMenuItem.Text = "文件(F)";
//
// 新建CtrlNToolStripMenuItem
//
this.新建CtrlNToolStripMenuItem.Name = "新建CtrlNToolStripMenuItem";
this.新建CtrlNToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.新建CtrlNToolStripMenuItem.Text = "新建 Ctrl+N";
this.新建CtrlNToolStripMenuItem.Click += new System.EventHandler(this.新建CtrlNToolStripMenuItem_Click);
//
// 打开OCtrlOToolStripMenuItem
//
this.打开OCtrlOToolStripMenuItem.Name = "打开OCtrlOToolStripMenuItem";
this.打开OCtrlOToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.打开OCtrlOToolStripMenuItem.Text = "打开(O).. Ctrl+O";
this.打开OCtrlOToolStripMenuItem.Click += new System.EventHandler(this.打开OCtrlOToolStripMenuItem_Click);
//
// 保存SCtrlSToolStripMenuItem
//
this.保存SCtrlSToolStripMenuItem.Name = "保存SCtrlSToolStripMenuItem";
this.保存SCtrlSToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.保存SCtrlSToolStripMenuItem.Text = "保存(S) Ctrl+S";
this.保存SCtrlSToolStripMenuItem.ToolTipText = "-";
this.保存SCtrlSToolStripMenuItem.Click += new System.EventHandler(this.保存SCtrlSToolStripMenuItem_Click);
//
// 另存为AToolStripMenuItem
//
this.另存为AToolStripMenuItem.Name = "另存为AToolStripMenuItem";
this.另存为AToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.另存为AToolStripMenuItem.Text = "另存为(A)...";
this.另存为AToolStripMenuItem.Click += new System.EventHandler(this.另存为AToolStripMenuItem_Click);
//
// toolStripSeparator4
//
this.toolStripSeparator4.Name = "toolStripSeparator4";
this.toolStripSeparator4.Size = new System.Drawing.Size(172, 6);
//
// 页面设置UToolStripMenuItem
//
this.页面设置UToolStripMenuItem.Name = "页面设置UToolStripMenuItem";
this.页面设置UToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.页面设置UToolStripMenuItem.Text = "页面设置(U)..";
//
// 打印ToolStripMenuItem
//
this.打印ToolStripMenuItem.Name = "打印ToolStripMenuItem";
this.打印ToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.打印ToolStripMenuItem.Text = "打印()";
this.打印ToolStripMenuItem.Click += new System.EventHandler(this.打印ToolStripMenuItem_Click);
//
// toolStripSeparator5
//
this.toolStripSeparator5.Name = "toolStripSeparator5";
this.toolStripSeparator5.Size = new System.Drawing.Size(172, 6);
//
// 退出ToolStripMenuItem
//
this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
this.退出ToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
this.退出ToolStripMenuItem.Text = "退出";
//
// 编辑EToolStripMenuItem
//
this.编辑EToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.撤销UCtrlZToolStripMenuItem,
this.toolStripSeparator1,
this.剪切TCtrlXToolStripMenuItem,
this.复制CCtrlCToolStripMenuItem,
this.粘贴PCtrlVToolStripMenuItem,
this.删除LToolStripMenuItem,
this.toolStripSeparator2,
this.查找FCtrlFToolStripMenuItem,
this.查找下一项NF3ToolStripMenuItem,
this.替换RCtrlHToolStripMenuItem,
this.转到GCtrlGToolStripMenuItem,
this.toolStripSeparator3,
this.全选ACtrlAToolStripMenuItem,
this.时间日期DF5ToolStripMenuItem});
this.编辑EToolStripMenuItem.Name = "编辑EToolStripMenuItem";
this.编辑EToolStripMenuItem.Size = new System.Drawing.Size(59, 21);
this.编辑EToolStripMenuItem.Text = "编辑(E)";
this.编辑EToolStripMenuItem.Click += new System.EventHandler(this.编辑EToolStripMenuItem_Click);
//
// 撤销UCtrlZToolStripMenuItem
//
this.撤销UCtrlZToolStripMenuItem.Name = "撤销UCtrlZToolStripMenuItem";
this.撤销UCtrlZToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.撤销UCtrlZToolStripMenuItem.Text = "撤销(U) Ctrl+Z";
this.撤销UCtrlZToolStripMenuItem.Click += new System.EventHandler(this.撤销UCtrlZToolStripMenuItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(177, 6);
//
// 剪切TCtrlXToolStripMenuItem
//
this.剪切TCtrlXToolStripMenuItem.Name = "剪切TCtrlXToolStripMenuItem";
this.剪切TCtrlXToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.剪切TCtrlXToolStripMenuItem.Text = "剪切(T) Ctrl+X";
this.剪切TCtrlXToolStripMenuItem.Click += new System.EventHandler(this.剪切TCtrlXToolStripMenuItem_Click);
//
// 复制CCtrlCToolStripMenuItem
//
this.复制CCtrlCToolStripMenuItem.Name = "复制CCtrlCToolStripMenuItem";
this.复制CCtrlCToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.复制CCtrlCToolStripMenuItem.Text = "复制(C) Ctrl+C";
this.复制CCtrlCToolStripMenuItem.Click += new System.EventHandler(this.复制CCtrlCToolStripMenuItem_Click);
//
// 粘贴PCtrlVToolStripMenuItem
//
this.粘贴PCtrlVToolStripMenuItem.Name = "粘贴PCtrlVToolStripMenuItem";
this.粘贴PCtrlVToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.粘贴PCtrlVToolStripMenuItem.Text = "粘贴(P) Ctrl+V";
this.粘贴PCtrlVToolStripMenuItem.Click += new System.EventHandler(this.粘贴PCtrlVToolStripMenuItem_Click);
//
// 删除LToolStripMenuItem
//
this.删除LToolStripMenuItem.Name = "删除LToolStripMenuItem";
this.删除LToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.删除LToolStripMenuItem.Text = "删除(L) ";
this.删除LToolStripMenuItem.Click += new System.EventHandler(this.删除LToolStripMenuItem_Click);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(177, 6);
//
// 查找FCtrlFToolStripMenuItem
//
this.查找FCtrlFToolStripMenuItem.Name = "查找FCtrlFToolStripMenuItem";
this.查找FCtrlFToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.查找FCtrlFToolStripMenuItem.Text = "查找(F) Ctrl+F";
this.查找FCtrlFToolStripMenuItem.Click += new System.EventHandler(this.查找FCtrlFToolStripMenuItem_Click);
//
// 查找下一项NF3ToolStripMenuItem
//
this.查找下一项NF3ToolStripMenuItem.Name = "查找下一项NF3ToolStripMenuItem";
this.查找下一项NF3ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.查找下一项NF3ToolStripMenuItem.Text = "查找下一项(N) F3";
this.查找下一项NF3ToolStripMenuItem.Click += new System.EventHandler(this.查找下一项NF3ToolStripMenuItem_Click);
//
// 替换RCtrlHToolStripMenuItem
//
this.替换RCtrlHToolStripMenuItem.Name = "替换RCtrlHToolStripMenuItem";
this.替换RCtrlHToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.替换RCtrlHToolStripMenuItem.Text = "替换(R) Ctrl+H";
this.替换RCtrlHToolStripMenuItem.Click += new System.EventHandler(this.替换RCtrlHToolStripMenuItem_Click);
//
// 转到GCtrlGToolStripMenuItem
//
this.转到GCtrlGToolStripMenuItem.Name = "转到GCtrlGToolStripMenuItem";
this.转到GCtrlGToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.转到GCtrlGToolStripMenuItem.Text = "转到(G) Ctrl+G";
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(177, 6);
//
// 全选ACtrlAToolStripMenuItem
//
this.全选ACtrlAToolStripMenuItem.Name = "全选ACtrlAToolStripMenuItem";
this.全选ACtrlAToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.全选ACtrlAToolStripMenuItem.Text = "全选(A) Ctrl+A";
this.全选ACtrlAToolStripMenuItem.Click += new System.EventHandler(this.全选ACtrlAToolStripMenuItem_Click);
//
// 时间日期DF5ToolStripMenuItem
//
this.时间日期DF5ToolStripMenuItem.Name = "时间日期DF5ToolStripMenuItem";
this.时间日期DF5ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.时间日期DF5ToolStripMenuItem.Text = "时间/日期(D) F5";
this.时间日期DF5ToolStripMenuItem.Click += new System.EventHandler(this.时间日期DF5ToolStripMenuItem_Click);
//
// 格式OToolStripMenuItem
//
this.格式OToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.自动换行ToolStripMenuItem,
this.字体FToolStripMenuItem});
this.格式OToolStripMenuItem.Name = "格式OToolStripMenuItem";
this.格式OToolStripMenuItem.Size = new System.Drawing.Size(62, 21);
this.格式OToolStripMenuItem.Text = "格式(O)";
//
// 自动换行ToolStripMenuItem
//
this.自动换行ToolStripMenuItem.Name = "自动换行ToolStripMenuItem";
this.自动换行ToolStripMenuItem.Size = new System.Drawing.Size(144, 22);
this.自动换行ToolStripMenuItem.Text = "自动换行(W)";
this.自动换行ToolStripMenuItem.Click += new System.EventHandler(this.自动换行ToolStripMenuItem_Click);
//
// 字体FToolStripMenuItem
//
this.字体FToolStripMenuItem.Name = "字体FToolStripMenuItem";
this.字体FToolStripMenuItem.Size = new System.Drawing.Size(144, 22);
this.字体FToolStripMenuItem.Text = "字体(F)";
this.字体FToolStripMenuItem.Click += new System.EventHandler(this.字体FToolStripMenuItem_Click);
//
// 查看VToolStripMenuItem
//
this.查看VToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.状态栏SToolStripMenuItem});
this.查看VToolStripMenuItem.Name = "查看VToolStripMenuItem";
this.查看VToolStripMenuItem.Size = new System.Drawing.Size(60, 21);
this.查看VToolStripMenuItem.Text = "查看(V)";
//
// 状态栏SToolStripMenuItem
//
this.状态栏SToolStripMenuItem.Name = "状态栏SToolStripMenuItem";
this.状态栏SToolStripMenuItem.Size = new System.Drawing.Size(127, 22);
this.状态栏SToolStripMenuItem.Text = "状态栏(S)";
//
// 帮助ToolStripMenuItem
//
this.帮助ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.查看帮助HToolStripMenuItem,
this.toolStripSeparator6,
this.关于记事本AToolStripMenuItem});
this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";
this.帮助ToolStripMenuItem.Size = new System.Drawing.Size(61, 21);
this.帮助ToolStripMenuItem.Text = "帮助(H)";
this.帮助ToolStripMenuItem.Click += new System.EventHandler(this.帮助ToolStripMenuItem_Click);
//
// 查看帮助HToolStripMenuItem
//
this.查看帮助HToolStripMenuItem.Name = "查看帮助HToolStripMenuItem";
this.查看帮助HToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.查看帮助HToolStripMenuItem.Text = "查看帮助(H)";
//
// toolStripSeparator6
//
this.toolStripSeparator6.Name = "toolStripSeparator6";
this.toolStripSeparator6.Size = new System.Drawing.Size(149, 6);
//
// 关于记事本AToolStripMenuItem
//
this.关于记事本AToolStripMenuItem.Name = "关于记事本AToolStripMenuItem";
this.关于记事本AToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.关于记事本AToolStripMenuItem.Text = "关于记事本(A)";
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 25);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.ShortcutsEnabled = false;
this.textBox1.Size = new System.Drawing.Size(1043, 540);
this.textBox1.TabIndex = 1;
this.textBox1.WordWrap = false;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1043, 565);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 文件FToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 新建CtrlNToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 打开OCtrlOToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 保存SCtrlSToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 另存为AToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 页面设置UToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 打印ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 编辑EToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 格式OToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查看VToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 撤销UCtrlZToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 剪切TCtrlXToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 复制CCtrlCToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 粘贴PCtrlVToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 删除LToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查找FCtrlFToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查找下一项NF3ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 替换RCtrlHToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 转到GCtrlGToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 全选ACtrlAToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 时间日期DF5ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 自动换行ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 字体FToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 状态栏SToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查看帮助HToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 关于记事本AToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator4;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator5;
private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator6;
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
}
}
下面是Form2事件的内容,这个是替换的对话框
下面是代码
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 记事本开发1._0
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string chazhaowenben;
public int index;
public int lastindex;
public int guangbiaoweizhi;
public string jishiwenben;
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
jishiwenben = form1.textBox1.Text;
chazhaowenben = textBox1.Text;
if (checkBox1.Checked == true)
{
index = jishiwenben.IndexOf(chazhaowenben);
}
else
{
index = jishiwenben.IndexOf(chazhaowenben, form1.guangbiao,StringComparison.CurrentCultureIgnoreCase);
}
Console.WriteLine(index);
if (index == -1)
{
MessageBox.Show(string.Format("查不到 {0}/或已经到文件末尾", chazhaowenben));
}
else
{
form1.textBox1.Select(index, chazhaowenben.Length);
form1.textBox1.Focus();
form1.guangbiao = index + chazhaowenben.Length;
}
}
private void button2_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
if (textBox2.Text != "")
{
form1.textBox1.SelectedText = textBox2.Text;
}
else
{
MessageBox.Show(null, "你还没有输入替换内容", "提示");
}
}
private void button3_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
jishiwenben = form1.textBox1.Text;
chazhaowenben = textBox1.Text;
do
{
if (checkBox1.Checked == true)
{
index = jishiwenben.IndexOf(chazhaowenben);
}
else
{
index = jishiwenben.IndexOf(chazhaowenben, form1.guangbiao, StringComparison.CurrentCultureIgnoreCase);
}
Console.WriteLine(index);
if (index == -1)
{
MessageBox.Show(string.Format("查不到 {0}/或已经到文件末尾", chazhaowenben));
}
else
{
form1.textBox1.Select(index, chazhaowenben.Length);
form1.textBox1.Focus();
form1.guangbiao = index + chazhaowenben.Length;
}
if (textBox2.Text != "")
{
form1.textBox1.SelectedText = textBox2.Text;
}
else
{
MessageBox.Show(null, "你还没有输入替换内容", "提示");
break;
}
} while (index >= 0);
}
}
}
namespace 记事本开发1._0
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(12, 54);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(125, 19);
this.label1.TabIndex = 0;
this.label1.Text = "查找内容(N):";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(12, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(116, 19);
this.label2.TabIndex = 1;
this.label2.Text = "替换为(P): ";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(143, 52);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 21);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(143, 99);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(216, 21);
this.textBox2.TabIndex = 3;
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(386, 45);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(153, 28);
this.button1.TabIndex = 4;
this.button1.Text = "查找下一项(F)";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.Location = new System.Drawing.Point(386, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(153, 34);
this.button2.TabIndex = 5;
this.button2.Text = "替换(R)";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button3.Location = new System.Drawing.Point(386, 155);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(153, 32);
this.button3.TabIndex = 6;
this.button3.Text = "全部替换(A)";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button4.Location = new System.Drawing.Point(389, 219);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(150, 37);
this.button4.TabIndex = 7;
this.button4.Text = "取消";
this.button4.UseVisualStyleBackColor = true;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.checkBox1.Location = new System.Drawing.Point(14, 243);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(153, 23);
this.checkBox1.TabIndex = 8;
this.checkBox1.Text = "区别大小写(C)";
this.checkBox1.UseVisualStyleBackColor = true;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(571, 328);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "替换";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.CheckBox checkBox1;
}
}
下面是Form3 的事件内容,为查找框里面的内容
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 记事本开发1._0
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public string chazhaowenben;
public int index;
public int lastindex;
public int guangbiaoweizhi;
public string jishiwenben;
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
jishiwenben = form1.textBox1.Text;
chazhaowenben = textBox1.Text;
if (checkBox1.Checked == true)
{
index = jishiwenben.IndexOf(chazhaowenben);
}
else
{
index = jishiwenben.IndexOf(chazhaowenben, form1.guangbiao, StringComparison.CurrentCultureIgnoreCase);
}
if (index == -1)
{
MessageBox.Show(string.Format("查不到 {0}/或已经到文件末尾", chazhaowenben));
}
else
{
form1.textBox1.Select(index,chazhaowenben.Length);
form1.textBox1.Focus();
form1.guangbiao = index + chazhaowenben.Length;
}
}
}
}
namespace 记事本开发1._0
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(23, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 0;
this.label1.Text = "查找内容(N):";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(106, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(232, 21);
this.textBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(358, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(95, 23);
this.button1.TabIndex = 2;
this.button1.Text = "查找下一项(F)";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(358, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(95, 25);
this.button2.TabIndex = 3;
this.button2.Text = "取消";
this.button2.UseVisualStyleBackColor = true;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(24, 113);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(102, 16);
this.checkBox1.TabIndex = 4;
this.checkBox1.Text = "区分大小写(C)";
this.checkBox1.UseVisualStyleBackColor = true;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(138, 60);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 100);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "方向";
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Checked = true;
this.radioButton2.Location = new System.Drawing.Point(115, 51);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(59, 16);
this.radioButton2.TabIndex = 1;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "向下()";
this.radioButton2.UseVisualStyleBackColor = true;
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(17, 49);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(65, 16);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "向上(U)";
this.radioButton1.UseVisualStyleBackColor = true;
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(477, 172);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form3";
this.Text = "查找";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
}
}