c#简单实现记事本功能
2015-10-23 22:32
381 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 进程和线程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false; //隐藏listbox,即历史记录
}
String[] path;
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择文本文件";
ofd.Multiselect = true;
ofd.Filter = "文本文件|*.txt";
ofd.ShowDialog();
path = ofd.FileNames;
if (path.Length <= 0)
{
return;
}
for (int i = 0; i < path.Length; i++)
{
listBox1.Items.Add(Path.GetFileName(path[i]));
}
ShowContent(0);
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择保存的文件夹";
sfd.InitialDirectory = @"c:\";
sfd.Filter = "文本文件|*.txt";
sfd.ShowDialog();
String fileName = sfd.FileName;
if(fileName == "")
{
return;
}
using (FileStream fwrite = new FileStream(fileName, FileMode.OpenOrCreate))
{
byte[] buf = new byte[1024 * 1024 * 5];
buf = Encoding.UTF8.GetBytes(textBox1.Text);
fwrite.Write(buf, 0, buf.Length);
}
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!textBox1.WordWrap)
{
textBox1.WordWrap = true;
自动换行ToolStripMenuItem.Text = "取消自动换行";
}
else
{
自动换行ToolStripMenuItem.Text = "取消自动换行";
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.ForeColor = cd.Color;
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible =
4000
false;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
ShowContent(listBox1.SelectedIndex);
}
private void ShowContent(int p)
{
using (FileStream fread = new FileStream(path[p], FileMode.Open))
{
byte[] buf = new byte[1024 * 1024 * 5];
fread.Read(buf, 0, buf.Length);
textBox1.Text = System.Text.Encoding.UTF8.GetString(buf);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 进程和线程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false; //隐藏listbox,即历史记录
}
String[] path;
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择文本文件";
ofd.Multiselect = true;
ofd.Filter = "文本文件|*.txt";
ofd.ShowDialog();
path = ofd.FileNames;
if (path.Length <= 0)
{
return;
}
for (int i = 0; i < path.Length; i++)
{
listBox1.Items.Add(Path.GetFileName(path[i]));
}
ShowContent(0);
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择保存的文件夹";
sfd.InitialDirectory = @"c:\";
sfd.Filter = "文本文件|*.txt";
sfd.ShowDialog();
String fileName = sfd.FileName;
if(fileName == "")
{
return;
}
using (FileStream fwrite = new FileStream(fileName, FileMode.OpenOrCreate))
{
byte[] buf = new byte[1024 * 1024 * 5];
buf = Encoding.UTF8.GetBytes(textBox1.Text);
fwrite.Write(buf, 0, buf.Length);
}
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!textBox1.WordWrap)
{
textBox1.WordWrap = true;
自动换行ToolStripMenuItem.Text = "取消自动换行";
}
else
{
自动换行ToolStripMenuItem.Text = "取消自动换行";
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.ForeColor = cd.Color;
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible =
4000
false;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
ShowContent(listBox1.SelectedIndex);
}
private void ShowContent(int p)
{
using (FileStream fread = new FileStream(path[p], FileMode.Open))
{
byte[] buf = new byte[1024 * 1024 * 5];
fread.Read(buf, 0, buf.Length);
textBox1.Text = System.Text.Encoding.UTF8.GetString(buf);
}
}
}
}
相关文章推荐
- C#学习日记30---泛型类、泛型方法、泛型委托
- C#修饰符详解
- string.Format对C#字符串格式化
- c#中的数据类型
- C#窗体随机四则运算 (第四次作业)
- C#使用aforge框架打开摄像头(续)
- C#属性和成员变量的区别?
- .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)
- C# Winform下载文件并显示进度条
- C#中如何设置窗体的默认按钮和取消按钮
- [C#]注册表操作
- C# 闭包问题-你被”坑“过吗?
- C#中HashTable的用法
- C# oleDb方法读取Excel文件
- C#异步编程(一):异步基础
- c#“集合已修改;可能无法执行枚举操作”
- c#分割习题
- 第三章 C#循环与方法
- 第二章 C#基本数据类型
- 第一章 用记事本搭建C#程序