.Net学习笔记----2015-07-02(记事本应用)

功能更多嘞~

  1     public partial class Form1 : Form
  2     {
  3         public Form1()
  4         {
  5             InitializeComponent();
  6         }
  7         //声明一个泛型集合用来打开文件的全路径
  8         List<string> list = new List<string>();
  9         private void Form1_Load(object sender, EventArgs e)
 10         {
 11             //加载程序的时候隐藏panel
 12             panel1.Visible = false;
 13             //取消文本框的自动换行功能
 14             textBox1.WordWrap = false;
 15         }
 16         /// <summary>
 17         /// 点击按钮的时候也要把panel隐藏
 18         /// </summary>
 19         /// <param name="sender"></param>
 20         /// <param name="e"></param>
 21         private void button1_Click(object sender, EventArgs e)
 22         {
 23             panel1.Visible = false;
 24         }
 25 
 26         private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
 27         {
 28             panel1.Visible = true;
 29         }
 30 
 31         private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
 32         {
 33             panel1.Visible = false;
 34         }
 35         /// <summary>
 36         /// 打开对话框
 37         /// </summary>
 38         /// <param name="sender"></param>
 39         /// <param name="e"></param>
 40         private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
 41         {
 42             OpenFileDialog ofd = new OpenFileDialog();
 43             ofd.Title = "请选择要打开的文件";
 44             ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";//初始目录
 45             ofd.Multiselect = true;//设置为多选
 46             ofd.Filter = "文本文件|*.txt|所有文件|*.*";
 47             ofd.ShowDialog();
 48 
 49             //获得用户选中的文件的路径
 50             string path = ofd.FileName;
 51             //将文件的全路径存储到泛型集合中
 52             list.Add(path);
 53             //获得用户打开文件的文件名
 54             string fileName = Path.GetFileName(path);
 55             //将文件名放到listbox中
 56             listBox1.Items.Add(fileName);
 57             if (path == "")
 58             {
 59                 return;
 60             }
 61             using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
 62             {
 63                 byte[] buffer = new byte[1024 * 1024 * 5];
 64                 int r = fsRead.Read(buffer, 0, buffer.Length);
 65                 textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
 66             }
 67         }
 68         /// <summary>
 69         /// 保存对话框
 70         /// </summary>
 71         /// <param name="sender"></param>
 72         /// <param name="e"></param>
 73         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
 74         {
 75             SaveFileDialog sfd = new SaveFileDialog();
 76             sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
 77             sfd.Title = "请选择要保存的文件路径";
 78             sfd.Filter = "文本文件|*.txt|所有文件|*.*";
 79             sfd.ShowDialog();
 80             //获得用户要保持的文件的路径
 81             string path = sfd.FileName;
 82             if (path == "")
 83             {
 84                 return;
 85             }
 86             using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
 87             {
 88                 byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
 89                 fsWrite.Write(buffer, 0, buffer.Length);
 90             }
 91             MessageBox.Show("保存成功");
 92         }
 93 
 94         private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
 95         {
 96             if (自动换行ToolStripMenuItem.Text == "自动换行")
 97             {
 98                 textBox1.WordWrap = true;
 99                 自动换行ToolStripMenuItem.Text = "取消自动换行";
100             }
101             else if (自动换行ToolStripMenuItem.Text == "取消自动换行")
102             {
103                 textBox1.WordWrap = false;
104                 自动换行ToolStripMenuItem.Text = "自动换行";
105             }
106         }
107 
108         private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
109         {
110             FontDialog fd = new FontDialog();
111             fd.ShowDialog();
112             textBox1.Font = fd.Font;
113         }
114 
115         private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
116         {
117             ColorDialog cd = new ColorDialog();
118             cd.ShowDialog();
119             textBox1.ForeColor = cd.Color;
120         }
121         /// <summary>
122         /// 双击打开对应的文件
123         /// </summary>
124         /// <param name="sender"></param>
125         /// <param name="e"></param>
126         private void listBox1_DoubleClick(object sender, EventArgs e)
127         {
128             //要获得双击的文件所对应的全路径
129             string path = list[listBox1.SelectedIndex];
130             using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
131             {
132                 byte[] buffer = new byte[1024 * 1024 * 5];
133                 int r = fsRead.Read(buffer, 0, buffer.Length);
134 
135                 textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
136             }
137         }
138     }

 

转载于:https://www.cnblogs.com/mikie/p/4616691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值