
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TXT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Modified)
{
/*提示保存对话框*/
DialogResult dResult = MessageBox.Show("文件" + this.Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (dResult)
{
case DialogResult.Yes:
另存为ToolStripMenuItem_Click(null, null);
richTextBox1.Clear();
this.Text = "无标题-记事本";
break;
case DialogResult.No:
richTextBox1.Clear();
this.Text = "无标题-记事本";
break;
case DialogResult.Cancel:
break;
}
}
else
{
richTextBox1.Clear();
this.Text = "无标题-记事本";
richTextBox1.Modified = false;
}
}
private static string openfilepath = ""; //保存所打开文件的路径
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型
openFileDialog1.FilterIndex = 1; //设置默认文件类型的显示顺序
openFileDialog1.RestoreDirectory = true; //打开对话框是否记忆上次打开的目录
StreamReader sr = null; //定义StreamReader对象
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
openfilepath = openFileDialog1.FileName; //获取打开的文件路径
string name = openfilepath.Substring(openfilepath.LastIndexOf("\\") + 1);
this.Text = name; //文件名作为标题
sr = new StreamReader(openfilepath, Encoding.Default); //实例化sr
richTextBox1.Text = sr.ReadToEnd(); //读取所有文件内容
}
catch
{
MessageBox.Show("打开文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
finally
{
if (sr != null)
{
sr.Close(); //关闭对象sr
sr.Dispose(); //释放对象sr资源
}
}
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型
saveFileDialog1.FilterIndex = 2; //设置默认文件类型的显示顺序
saveFileDialog1.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
openfilepath = saveFileDialog1.FileName.ToString(); //获取文件路径
FileStream fs;
try
{ fs = File.Create(openfilepath); }
catch
{
MessageBox.Show("建立文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
byte[] content = Encoding.Default.GetBytes(richTextBox1.Text);
try
{
fs.Write(content, 0, content.Length);
fs.Flush();
toolStripStatusLabel1.Text = "保存成功";
}
catch
{
MessageBox.Show("写入文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
fs.Close();
fs.Dispose();
}
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
StreamWriter sw = null;
if (openfilepath == "")
{
另存为ToolStripMenuItem_Click(null, null); //调用另存为方法
return;
}
try
{
sw = new StreamWriter(openfilepath, false, Encoding.Default);
sw.Write(richTextBox1.Text);
toolStripStatusLabel1.Text = "保存成功";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", System.Windows.Forms.MessageBoxButtons.
OK, System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
finally
{
if (sw != null)
{
sw.Close(); //关闭StreamWriter
sw.Dispose(); //释放资源
}
}
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.WordWrap == true)
{
richTextBox1.WordWrap = false;
自动换行ToolStripMenuItem.Checked = false;
richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
}
else
{
richTextBox1.WordWrap = true;
自动换行ToolStripMenuItem.Checked = true;
richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedVertical;
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
Font font = fontDialog1.Font;
richTextBox1.SelectionFont = font;
}
}
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (statusStrip1.Visible == true)
{
statusStrip1.Visible = false;
状态栏SToolStripMenuItem.Checked = false;
richTextBox1.Height += 22;
}
else
{
statusStrip1.Visible = true;
状态栏SToolStripMenuItem.Checked = true;
richTextBox1.Height -= 22;
}
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.richtextbox = richTextBox1;
f2.ShowDialog();
}
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
place();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
place();
}
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
place();
}
private void place() //计算行数与列数
{
string str = this.richTextBox1.Text;
int m = this.richTextBox1.SelectionStart;
int Ln = 0;
int Col = 0;
for (int i = m - 1; i >= 0; i--)
{
if (str[i] == '\n')
Ln++;
if (Ln < 1)
Col++;
}
Ln = Ln + 1;
Col = Col + 1;
toolStripStatusLabel1.Text = "行:" + Ln.ToString() + "," + "列:" + Col.ToString();
}
private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.richText = richTextBox1;
f3.ShowDialog();
}
private void 撤销UToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = "";
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
{
richTextBox1.SelectedText = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.
Second.ToString() + " " + DateTime.Now.Year.ToString() + "-" + DateTime.
Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
}
else
{
richTextBox1.SelectedText += DateTime.Now.Hour.ToString() + ":" + DateTime.
Now.Second.ToString() + " " + DateTime.Now.Year.ToString() + "-" + DateTime.
Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
}
}
private void 关于记事本GToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.Show();
}
private void toolStripButton1_Click(object sender, EventArgs e) //新建
{
if (richTextBox1.Modified)
{
/*提示保存对话框*/
DialogResult dResult = MessageBox.Show("文件" + this.Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (dResult)
{
case DialogResult.Yes:
另存为ToolStripMenuItem_Click(null, null);
richTextBox1.Clear();
this.Text = "无标题-记事本";
break;
case DialogResult.No:
richTextBox1.Clear();
this.Text = "无标题-记事本";
break;
case DialogResult.Cancel:
break;
}
}
else
{
richTextBox1.Clear();
this.Text = "无标题-记事本";
richTextBox1.Modified = false;
}
}
private void toolStripButton2_Click(object sender, EventArgs e) //打开
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型
openFileDialog1.FilterIndex = 1; //设置默认文件类型的显示顺序
openFileDialog1.RestoreDirectory = true; //打开对话框是否记忆上次打开的目录
StreamReader sr = null; //定义StreamReader对象
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
openfilepath = openFileDialog1.FileName; //获取打开的文件路径
string name = openfilepath.Substring(openfilepath.LastIndexOf("\\") + 1);
this.Text = name; //文件名作为标题
sr = new StreamReader(openfilepath, Encoding.Default); //实例化sr
richTextBox1.Text = sr.ReadToEnd(); //读取所有文件内容
}
catch
{
MessageBox.Show("打开文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
finally
{
if (sr != null)
{
sr.Close(); //关闭对象sr
sr.Dispose(); //释放对象sr资源
}
}
}
}
private void toolStripButton3_Click(object sender, EventArgs e) //保存
{
StreamWriter sw = null;
if (openfilepath == "")
{
另存为ToolStripMenuItem_Click(null, null); //调用另存为方法
return;
}
try
{
sw = new StreamWriter(openfilepath, false, Encoding.Default);
sw.Write(richTextBox1.Text);
toolStripStatusLabel1.Text = "保存成功";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", System.Windows.Forms.MessageBoxButtons.
OK, System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
finally
{
if (sw != null)
{
sw.Close(); //关闭StreamWriter
sw.Dispose(); //释放资源
}
}
}
private void toolStripButton4_Click(object sender, EventArgs e) //剪切
{
richTextBox1.Cut();
}
private void toolStripButton5_Click(object sender, EventArgs e) //粘贴
{
richTextBox1.Paste();
}
private void toolStripButton6_Click(object sender, EventArgs e) //另存为
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设置文件类型
saveFileDialog1.FilterIndex = 2; //设置默认文件类型的显示顺序
saveFileDialog1.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
openfilepath = saveFileDialog1.FileName.ToString(); //获取文件路径
FileStream fs;
try
{ fs = File.Create(openfilepath); }
catch
{
MessageBox.Show("建立文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
byte[] content = Encoding.Default.GetBytes(richTextBox1.Text);
try
{
fs.Write(content, 0, content.Length);
fs.Flush();
toolStripStatusLabel1.Text = "保存成功";
}
catch
{
MessageBox.Show("写入文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
fs.Close();
fs.Dispose();
}
}
}
private void toolStripButton7_Click(object sender, EventArgs e) //查找
{
Form2 f2 = new Form2();
f2.richtextbox = richTextBox1;
f2.ShowDialog();
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.richText = richTextBox1;
f3.ShowDialog();
}
}
}
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 TXT
{
public partial class Form2 : Form
{
public RichTextBox richtextbox;
public int start = 0;
public Form2()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
richtextbox.SelectionColor = Color.Blue; //显示为蓝色
string str;
str = textBox1.Text;
if (checkBox1.Checked)
{
if (radioButton2.Checked)
{ checkUp(str); }
else
{ checkDown(str); }
}
else
{
if (radioButton1.Checked)
{ uncheckDown(str); }
else
{ uncheckUp(str); }
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
public void checkDown(string ss) //区分大小写向下查找
{
int c = 0;
int b = 0;
try
{
c = richtextbox.SelectionStart;
b = richtextbox.Text.IndexOf(ss, c + ss.Length, StringComparison.CurrentCulture);
richtextbox.SelectionStart = b;
richtextbox.SelectionLength = ss.Length;
richtextbox.SelectionColor = Color.Red; //显示为红色
}
catch
{
MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
MessageBoxButtons.OK);
this.textBox1.SelectionStart = c;
this.textBox1.SelectionLength = ss.Length;
}
}
public void checkUp(string ss) //区分大小写向上查找
{
int c = 0;
int b = 0;
try
{
c = richtextbox.SelectionStart;
b = richtextbox.Text.LastIndexOf(ss, c - ss.Length, StringComparison.InvariantCulture);
richtextbox.SelectionStart = b;
richtextbox.SelectionLength = ss.Length;
richtextbox.SelectionColor = Color.Red; //显示为红色
}
catch
{
MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
MessageBoxButtons.OK);
richtextbox.SelectionStart = c;
richtextbox.SelectionLength = ss.Length;
}
}
public void uncheckDown(string ss) //不区分大小写向下查找
{
int c = 0;
int b = 0;
try
{
c = richtextbox.SelectionStart;
b = richtextbox.Text.IndexOf(ss, c + ss.Length, StringComparison.CurrentCultureIgnoreCase);
richtextbox.SelectionStart = b;
richtextbox.SelectionLength = ss.Length;
richtextbox.SelectionColor = Color.Red; //显示为红色
}
catch
{
MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
MessageBoxButtons.OK);
richtextbox.SelectionStart = c;
richtextbox.SelectionLength = ss.Length;
}
}
public void uncheckUp(string ss) //不区分大小写向上查找
{
int c = 0;
int b = 0;
try
{
c = richtextbox.SelectionStart;
b = richtextbox.Text.LastIndexOf(ss, c - ss.Length,StringComparison.InvariantCultureIgnoreCase);
richtextbox.SelectionStart = b;
richtextbox.SelectionLength = ss.Length;
richtextbox.SelectionColor = Color.Red; //显示为红色
}
catch
{
MessageBox.Show("已查找到文档的结尾", "查找结束对话框",
MessageBoxButtons.OK);
richtextbox.SelectionStart = c;
richtextbox.SelectionLength = ss.Length;
}
}
}
}
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 TXT
{
public partial class Form3 : Form
{
public int start = 0;
public RichTextBox richText;
public Form3()
{
InitializeComponent();
}
public Form3(RichTextBox rtb)
{
richText = rtb;
}
private void button1_Click(object sender, EventArgs e)
{
string str1; //存放要查找的文本
str1 = textBox1.Text.Trim();
richText.SelectionColor = Color.Blue; //显示为蓝色
start = richText.Find(str1, start, RichTextBoxFinds.MatchCase); //查找下一个
if (start==1){
MessageBox.Show("已查找到文档的结尾", "查找结束对话框", MessageBoxButtons.OK);
start = 0;
}
else{
start = start + str1.Length;
}
richText.SelectionColor = Color.Red; //显示为红色
richText.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
string str1, str2;
str1 = textBox1.Text;
str2 = textBox2.Text;
richText.SelectionColor = Color.Blue; //显示为蓝色
start = richText.Find(str1, start, RichTextBoxFinds.MatchCase);
if (start==1)
{
MessageBox.Show("已替换到文档的结尾", "替换结束对话框", MessageBoxButtons.OK);
start = 0;
}
else
{
start = start + str1.Length;
richText.SelectedText = str2;
}
richText.SelectionColor = Color.Red; //显示为红色
richText.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
string str1, str2;
str1 = textBox1.Text;
str2 = textBox2.Text;
start = 0;
start = richText.Find(str1, start, RichTextBoxFinds.MatchCase);
while (start!=1)
{
richText.SelectedText = str2;
start += str2.Length;
start = richText.Find(str1, start, RichTextBoxFinds.MatchCase);
}
MessageBox.Show("已替换到文档的结尾", "替换结束对话框", MessageBoxButtons.OK);
start = 0;
richText.Focus();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
}
}