C#打造的简易记事本

X年前,听老一辈家说在选择方向性这个问题上一定要深思熟虑、要专一,这样才能把学问做的比较精通,从而才能有所作有所为。而几年过去了,学习虽然不曾间断过 但所作所为却是遍地杀花,每隔一段时间就另起炉灶,弄的自己跟一个万金油似的,最终搞的什么都成了浮云……

记事本相信大家都见过,用过,操蛋过,这里我也就趁着回滚学习了下C# 来演练一下它的基本实现方法

首先 应该要知道基本功能吧:打开-> 保存-> 复制-> 粘贴-> 剪切-> 全选-> 设置字体-> 自动换行 等

其次 整个窗体的布局可以分为菜单栏和富文本  顺加一个鼠标右键

最后 就是各个按钮的响应事件了

用过VS的人都知道用窗体自带控件比什么都来的实在 所以我们就先把整个架构搭起


主要控件列表:ContextMenuStrip(鼠标右键菜单)、MenuStrip(菜单栏)、  RichTextBox(富文本)、  FontDialog(字体对话框) 、OpenFileDialog(打开文件对话框)  、SaveFileDialog(保存文件对话框)

主要代码实现:

\\在编写代码前注意改下控件的NAME属性 这就同网页标签的ID一样 嘿重要

private void note_open_Click(object sender, EventArgs e)
{
if (note_openfile.ShowDialog() == DialogResult.OK)\\对话框是否打开
{
StreamReader sr = new StreamReader(note_openfile.FileName); \\以字节流方式打开文件
note_richtext.Text = sr.ReadLine();\\把文件内容赋值给文本
sr.Close();\\注意关闭
}
}

private void note_save_Click(object sender, EventArgs e)
{
if (note_savefile.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(note_savefile.FileName);\\以字节流的方式写入文件
sw.WriteLine(note_richtext.Text);
sw.Flush();\\清空字节流
sw.Close();
}
}

private void note_cut_Click(object sender, EventArgs e) {    note_richtext.Cut(); }\\实现剪切
private void note_copy_Click(object sender, EventArgs e) {   note_richtext.Copy();  }\\实现复制
private void note_paste_Click(object sender, EventArgs e)  {  note_richtext.Paste();  }\\实现粘贴
private void note_select_Click(object sender, EventArgs e) {   note_richtext.SelectAll(); }\\实现全选

private void note_line_Click(object sender, EventArgs e)
{
\\默认自动换行为TRUE 所以需要我们人工设置下
if (note_richtext.WordWrap)
note_richtext.WordWrap = false;
else   note_richtext.WordWrap = true;
}

private void note_setfont_Click(object sender, EventArgs e)
{
note_font.ShowApply = true;
note_font.ShowColor = true;
\\让其记住我们的字体样式
note_font.Color = note_richtext.ForeColor;
note_font.Font = note_richtext.Font;
if (note_font.ShowDialog() == DialogResult.OK)
{
note_richtext.ForeColor = note_font.Color;\\把选中字体颜色赋值给文本
note_richtext.Font = note_font.Font;\\把选中字体样式赋值给文本
}
}

private void note_exit_Click(object sender, EventArgs e)  {Application.Exit();   }\\退出程序

下图为程序的实现 基本还能用 ( 如果仔细一点的朋友们 应该会发现还有一个打印功能的代码没有贴出来 这个就留给有兴趣的朋友们自行研究)

好了 大致的东西其实并不是很困难 只是没有仔细去琢磨罢了 望从事互联网职业的大侠们 共勉励

PS:很久没有写以技术题材的文章了 总感觉挺累的 比码字更费手力和脑力 哎 洗洗睡去。。。

 

15 双脚印 杠杠的~ 我要评论
  1. Way April 14, 2011 at 09:03 1L

    哎呀,好多年前也写过,代码好熟悉,但是现在肯定写不出来了!

    Reply

    Roam April 18th, 2011 at 21:36

    看来是前辈了

    Reply

  2. 营养品 April 14, 2011 at 18:27 2L

    我也觉得代码熟悉 像天书

    Reply

  3. 西风 April 14, 2011 at 21:44 3L

    羡慕啊,就给我源代码 我都编辑不了

    Reply

    Roam April 18th, 2011 at 21:37

    学习是一个不能间断的过程 当然我指的不是 上面的代码 呵呵

    Reply

  4. 媛诺诺 April 15, 2011 at 09:23 4L

    代码对我来说太复杂

    Reply

    Roam April 18th, 2011 at 21:37

    那什么来说 比较对你胃口了

    Reply

  5. 营养品 April 15, 2011 at 09:48 5L

    方便快捷真的是真谛!!

    Reply

  6. 貔貅 April 15, 2011 at 23:51 6L

    一如既往的支持!

    Reply

  7. 飞一般的蜗牛 April 16, 2011 at 11:28 7L

    哈哈,我可以考虑做毕业设计

    Reply

    Roam April 18th, 2011 at 21:38

    没问题的

    Reply

  8. skyma April 17, 2011 at 22:49 8L

    小洁,不错呀,最近开始搞这个玩意了·····看了你最近工作挺闲的哦····我羡慕····

    Reply

    Roam April 18th, 2011 at 21:38

    没有闲 只是希望能多多充充电 因为不能落你太远

    Reply

  9. coach outlet wallets April 22, 2011 at 15:32 9L

    现在毕设怎么都在博客上找呀!!!

    Reply

  10. Afio May 11, 2011 at 13:11 10L

    正在学C#的我只能说这个记事本太水了….不过发现一个新玩意,我们貌似没用过关闭sr.Close();

    Reply