C#简单记事本源码,初学者可参考
自己边学边发的,大佬们嘴下留情{:301_1010:}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _72.记事本应用程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;//点击<<按钮的时候也隐藏panel
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false;//加载程序的时候隐藏panel
textBox1.WordWrap = false;//加载程序的时候取消自动换行
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true;//点击显示的时候显示panel
}
private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = false;//点击隐藏panel
}
List<string> list = new List<string>();
/// <summary>
/// 打开对话框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fod = new OpenFileDialog();
fod.Title = "请选择要打开的文本文件";
fod.InitialDirectory = @"C:\Users\Administrator\Desktop";//选择文件初始化的目录
fod.Multiselect = true;//设置为可多选
fod.Filter = "文本文件|*.txt|所有文件|*.*";//设定可选择文件类型
fod.ShowDialog();
//获得用户选中的文件的路径
string path = fod.FileName;
string wjm = Path.GetFileName(path);//重文件全路径中获取名字
listBox1.Items.Add(wjm);
//将文件的全路径存到泛型集合中
list.Add(path);
//判断获取路径是否为空(用户没有选择文件)
if (path=="")
{
return;
}
using (FileStream a = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] AS = new byte;//一次读取5m数据
int r = a.Read(AS,0,AS.Length);
textBox1.Text = Encoding.UTF8.GetString(AS,0,r);//解码
}
}
/// <summary>
/// 保存对话框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog bc = new SaveFileDialog();
bc.InitialDirectory = @"C:\Users\Administrator\Desktop";
bc.Title = "请选择要保存的文件路径";
bc.Filter = "文本文档|*.txt|全部类型|*.*";
bc.ShowDialog();
//获得用户要保存的文件路径
string path = bc.FileName;
if (path=="")
{
return;//如果为空则什么都不做(如果缺少这个程序在某些情况下就会报错如:用户取消保存)
}
using (FileStream xie = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] bs = Encoding.UTF8.GetBytes(textBox1.Text);
xie.Write(bs, 0, bs.Length);
}
MessageBox.Show("保存成功");
}
/// <summary>
/// 菜单自动换行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动换行ToolStripMenuItem.Text == "☆自动换行")
{
textBox1.WordWrap = true;
自动换行ToolStripMenuItem.Text = "★取消自动换行";
}
else if (自动换行ToolStripMenuItem.Text == "★取消自动换行")
{
textBox1.WordWrap = false;
自动换行ToolStripMenuItem.Text = "☆自动换行";
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog zt = new FontDialog();
zt.ShowDialog();
textBox1.Font= zt.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog ys = new ColorDialog();
ys.ShowDialog();
textBox1.ForeColor = ys.Color;
}
/// <summary>
/// 双击打开对应的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//获得双击文件对应的文件全路径
string path= list;
using (FileStream ds = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] bf = new byte;
int r= ds.Read(bf, 0, bf.Length);
textBox1.Text = Encoding.UTF8.GetString(bf,0,r);
}
}
}
} 沙发沙发 不知道干什么用的 支持一下
支持一波先。仅代表刺客大军前来问候 貌似挺可以的,看看先 学习了,收藏起来 拿走学习参考,谢谢楼主{:1_893:} 先收藏一波 有点好奇用途