C#编写记事本(高仿)

时间:2021-11-14 19:35:44

近一周写的关于记事本的代码,高仿记事本。本人C#入门不久,其中存在代码冗余,但懒得修改了。

经测试运行正常。

一、主窗体设计及代码

C#编写记事本(高仿)

 namespace BestEditor
{
public partial class Main : Form
{
private bool isTextChanged;
private string path;//记录文件路径(刚新建的文件路径为"",打开的文件路径为原路径) public Main()
{
InitializeComponent();
this.Text = "无标题 - 记事本";
path = "";
} /// <summary>
/// 初始化窗体时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Main_Load(object sender, EventArgs e)
{
//初始化,撤销、剪切、复制、删除 不可用
撤消UToolStripMenuItem.Enabled = false;
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
删除LToolStripMenuItem.Enabled = false; if (richTextBoxBoard.Equals(""))
{
查找FToolStripMenuItem.Enabled = false;
查找下一个NToolStripMenuItem.Enabled = false;
}
else
{
查找FToolStripMenuItem.Enabled = true;
查找下一个NToolStripMenuItem.Enabled = true;
} if (Clipboard.ContainsText())
粘贴PToolStripMenuItem.Enabled = true;
else
粘贴PToolStripMenuItem.Enabled = false; toolStripStatusLabel2.Text = "第 1 行,第 1 列";
} private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
//如果输入框文字发生变动
if (isTextChanged)
{
saveFileDialog1.FileName = "*.txt";
DialogResult dr = MessageBox.Show("是否将更改保存到 " + this.Text + "?", "记事本",
MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
//获取或设置指定要在 SaveFileDialog 中显示的文件类型和说明的筛选器字符串
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
richTextBoxBoard.Text = "";
path = "";
}
}
else if(dr == DialogResult.No)
{
richTextBoxBoard.Text = "";
path = "";
}
}
else
{
richTextBoxBoard.Text = "";
this.Text = "无标题 - 记事本";
path = "";
}
} /// <summary>
/// 输入框发生变化时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void richTextBoxBoard_TextChanged(object sender, EventArgs e)
{
isTextChanged = true;
} private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isTextChanged)
{
saveFileDialog1.FileName = "*.txt";
DialogResult dr = MessageBox.Show("是否将更改保存到 " + this.Text + "?", "记事本",
MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
//获取或设置指定要在 SaveFileDialog 中显示的文件类型和说明的筛选器字符串
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
Text = saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf("\\")+)+
" - 记事本";
}
}
} openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
path = openFileDialog1.FileName;
Text = path.Substring(path.LastIndexOf("\\") + ) + " - 记事本";
Console.WriteLine("path={0}",path);
richTextBoxBoard.LoadFile(path, RichTextBoxStreamType.PlainText);
isTextChanged = false;
}
} private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!("".Equals(path)))
{
richTextBoxBoard.SaveFile(path, RichTextBoxStreamType.PlainText);
isTextChanged = false;
}
else
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
path = saveFileDialog1.FileName;
this.Text = path.Substring(path.LastIndexOf("\\") + ) + " - 记事本";
isTextChanged = false;
}
}
} private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
path = saveFileDialog1.FileName;
this.Text = path.Substring(path.LastIndexOf("\\") + ) + " - 记事本";
isTextChanged = false;
}
} private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
} private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
printDialog1.ShowDialog();
} private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
} private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxBoard.Undo();
} private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBoxBoard.CanUndo)
撤消UToolStripMenuItem.Enabled = true; if (richTextBoxBoard.SelectionLength > )
{
剪切TToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem.Enabled = true;
删除LToolStripMenuItem.Enabled = true;
}
else
{
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
删除LToolStripMenuItem.Enabled = false;
} if (richTextBoxBoard.Equals(""))
{
查找FToolStripMenuItem.Enabled = false;
查找下一个NToolStripMenuItem.Enabled = false;
}
else
{
查找FToolStripMenuItem.Enabled = true;
查找下一个NToolStripMenuItem.Enabled = true;
} if (Clipboard.ContainsText())
粘贴PToolStripMenuItem.Enabled = true;
else
粘贴PToolStripMenuItem.Enabled = false;
} private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxBoard.Cut();
} private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxBoard.Copy();
} private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxBoard.Paste();
} private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxBoard.SelectedText = "";
} /// <summary>
/// 不同窗体之间通讯
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
{
Search search = new Search();
search.Owner = this;
search.Show();
} private void 查找下一个NToolStripMenuItem_Click(object sender, EventArgs e)
{
Search search = new Search();
search.Owner = this;
search.Show();
} private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
{
Change change = new Change();
change.Owner = this;
change.Show();
} private void 转到GToolStripMenuItem_Click(object sender, EventArgs e)
{
Goto gt = new Goto();
gt.Owner = this;
gt.Show();
} private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBoxBoard.SelectAll();
} private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
{
string front = richTextBoxBoard.Text.Substring(, richTextBoxBoard.SelectionStart);
string back = richTextBoxBoard.Text.Substring(richTextBoxBoard.SelectionStart,
richTextBoxBoard.Text.Length - richTextBoxBoard.SelectionStart);
richTextBoxBoard.Text = front + DateTime.Now.ToString() + back;
} private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBoxBoard.WordWrap)
{
自动换行WToolStripMenuItem.Checked = false;
richTextBoxBoard.WordWrap = false;
}
else
{
自动换行WToolStripMenuItem.Checked = true;
richTextBoxBoard.WordWrap = true;
}
} private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
richTextBoxBoard.SelectionFont = fontDialog1.Font;
} /// <summary>
/// 控制底部状态栏显示与否
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 状态栏SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (状态栏SToolStripMenuItem.Checked)
{
状态栏SToolStripMenuItem.Checked = false;
statusStrip1.Visible = false;
}
else
{
状态栏SToolStripMenuItem.Checked = true;
statusStrip1.Visible = true;
}
} /// <summary>
/// 输入框光标位置变化时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void richTextBoxBoard_SelectionChanged(object sender, EventArgs e)
{
string[] str = richTextBoxBoard.Text.Split('\r', '\n');
int row = , column = , pos = richTextBoxBoard.SelectionStart; foreach(string s in str)
Console.WriteLine(s);
Console.WriteLine("pos={0}",pos); for (int i = ; pos - str[i].Length > ; i++)
{
pos = pos - str[i].Length - ;
row = i + ;
}
column = pos + ;
toolStripStatusLabel2.Text = "第 " + row + " 行,第 " + column + " 列";
} private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox ab = new AboutBox();
ab.Show();
} private void 查看帮助HToolStripMenuItem_Click(object sender, EventArgs e)
{
//调用系统内部的notepad.chm文件
} /// <summary>
/// 点击窗体右上角关闭按钮触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (isTextChanged)
{
if (!("".Equals(path)))
{
DialogResult dr = MessageBox.Show("是否将更改保存到"+path+"?","记事本",
MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
richTextBoxBoard.SaveFile(path, RichTextBoxStreamType.PlainText);
else if (dr == DialogResult.No)
e.Cancel = false;
else
e.Cancel = true;//不关闭
}
else
{
DialogResult dr = MessageBox.Show("是否将更改保存到 无标题?", "记事本",
MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
else
e.Cancel = true;
}
else if (dr == DialogResult.No)
e.Cancel = false;
else
e.Cancel = true;
}
}
}
}
}

二、查找子窗体设计及代码

C#编写记事本(高仿)

 namespace BestEditor
{
public partial class Search : Form
{
private RichTextBox rtb = new RichTextBox(); public Search()
{
InitializeComponent();
} /// <summary>
/// 初始化时得到主窗口的通讯数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Search_Load(object sender, EventArgs e)
{
Main main = (Main)this.Owner;
this.rtb = main.richTextBoxBoard;
} /// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
this.Close();
} private void button1_Click(object sender, EventArgs e)
{
string str = rtb.Text;//文件内容
string subSearch = textBox1.Text;//查找内容
string initString = subSearch;
int pos = rtb.SelectionStart;//光标位置 if (!checkBox1.Checked)
{
str = str.ToLower();
subSearch = subSearch.ToLower();
} if (radioButton1.Checked)//向上查找
{
if (rtb.SelectionLength > )
pos = pos + rtb.SelectionLength - ; str = str.Substring(, pos);
if (subSearch != "" && (pos = str.LastIndexOf(subSearch, pos)) != -)
{
//输入框得到焦点并选中查找的内容
rtb.Focus();
rtb.SelectionStart = pos;
rtb.SelectionLength = subSearch.Length;
}
else
MessageBox.Show("找不到\"" + initString + "\"", "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (rtb.SelectionLength > )
pos = pos + ;
if (subSearch != "" && (pos = str.IndexOf(subSearch, pos)) != -)
{
rtb.Focus();
rtb.SelectionStart = pos;
rtb.SelectionLength = subSearch.Length;
}
else
MessageBox.Show("找不到\"" + subSearch + "\"", "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}

三、替换子窗体设计及其代码

C#编写记事本(高仿)

 namespace BestEditor
{
public partial class Change : Form
{
private RichTextBox rtb = new RichTextBox(); public Change()
{
InitializeComponent();
} private void Change_Load(object sender, EventArgs e)
{
Main main = (Main)this.Owner;
this.rtb = main.richTextBoxBoard;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
} /// <summary>
/// 查找下一个
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string str = rtb.Text;
string subSearch = textBox1.Text;
string initString = subSearch;
int pos = rtb.SelectionStart; if (!checkBox1.Checked)
{
str = str.ToLower();
subSearch = subSearch.ToLower();
} if (rtb.SelectionLength > )
pos = pos + ;
if ((pos = str.IndexOf(subSearch, pos)) != -)
{
rtb.Focus();
rtb.SelectionStart = pos;
rtb.SelectionLength = subSearch.Length;
}
else
MessageBox.Show("找不到\"" + initString + "\"", "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Information);
} private void textBox1_TextChanged(object sender, EventArgs e)
{
bool flag = textBox1.Text != "";
button1.Enabled = flag;
button2.Enabled = flag;
button3.Enabled = flag;
} /// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 替换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string str = rtb.Text;
string subSearch = textBox1.Text;
string initString = subSearch;
string changeTo = textBox2.Text;
string front;
string dest;
string back;
int pos = rtb.SelectionStart; if (!checkBox1.Checked)
{
str = str.ToLower();
subSearch = subSearch.ToLower();
} if (rtb.SelectionLength > )
{
if (rtb.SelectedText.Equals(subSearch))
{
//将文本框字符串分段,替换后再组合
front = rtb.Text.Substring(,pos);
dest = changeTo;
back = rtb.Text.Substring(pos + subSearch.Length, rtb.Text.Length - pos - subSearch.Length);
rtb.Text = front + dest + back;
}
pos = pos + ;
} if ((pos = str.IndexOf(subSearch, pos)) != -)
{
rtb.Focus();
rtb.SelectionStart = pos;
rtb.SelectionLength = subSearch.Length;
}
else
MessageBox.Show("找不到\"" + initString + "\"", "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Information);
} /// <summary>
/// 全部替换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
string str = rtb.Text;
string subSearch = textBox1.Text;
string changeTo = textBox2.Text;
string front;
string dest;
string back;
int pos = ; if (!checkBox1.Checked)
{
str = str.ToLower();
subSearch = subSearch.ToLower();
} while ((pos=str.IndexOf(subSearch,pos))!=-)
{
front = rtb.Text.Substring(, pos);
dest = changeTo;
back = rtb.Text.Substring(pos + subSearch.Length, rtb.Text.Length - pos - subSearch.Length);
rtb.Text = front + dest + back;
if (!checkBox1.Checked)
str = rtb.Text.ToLower();
}
}
}
}

四、转到子窗体设计及其代码

C#编写记事本(高仿)

 namespace BestEditor
{
public partial class Goto : Form
{
private RichTextBox rtb = new RichTextBox(); public Goto()
{
InitializeComponent();
} private void Goto_Load(object sender, EventArgs e)
{
Main main = (Main)this.Owner;
this.rtb = main.richTextBoxBoard;
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < || e.KeyChar > ) && e.KeyChar != && e.KeyChar != )
{
e.Handled = true;
MessageBox.Show("只能接收数字","记事本",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 确定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int row = int.Parse(textBox1.Text);
int pos = ;
string[] str = rtb.Text.Split('\r', '\n'); if (row < || row > str.Length)
MessageBox.Show("行数超出范围", "记事本 - 跳行", MessageBoxButtons.OK);
else
{
for (int i = ; i < row; i++)
pos = pos + str[i-].Length + ;
this.Close();
rtb.Focus();
rtb.SelectionStart = pos;
}
}
}
}

五、“关于”对话框

直接插入“关于”对话框即可

C#编写记事本(高仿)

代码下载链接:

http://pan.baidu.com/s/1o6mJtSu

Ps:

编写过程中有参考到以下代码:http://blog.csdn.net/mathewsking/article/details/3645753,在此谢谢博主的思路启发!