首先,先來一張效果圖。

1.兩個button控件------實現打開和保存功能。
2.對應的“對話框”--openFileDialog和SaveFileDialog控件。
3.一個textbox控件,為文本內容。
3.一個timer控件提示時間。
4.一個label控件,提示信息。
1 private void btn_open_Click(object sender, EventArgs e)
2 {
3 if (openFileDialog1.ShowDialog()==DialogResult.OK)
4 {
5 FileName = openFileDialog1.FileName;
6
7 try
8 {
9 OriginalContent = File.ReadAllText(FileName);
10 textBox1.Text = OriginalContent;
11 }
12 catch
13 {
14
15 lbl_notice.Text = "文件無法打開!";
16 }
17 }
18 }
寫了一個方法,便於直接保存和關閉保存。
1 private void save()
2 {
3 //當內容已改變時,此標記為True
4 bool ShouldSvae = false;
5 //如果文件名不為空,表明當前時文本框中的內容是來自於文件
6 if (FileName!="")
7 {
8 //如果內容改變
9 if (textBox1.Text!=OriginalContent&&MessageBox.Show("文件已修改,是否保存?" , "保存文件" , MessageBoxButtons.YesNo)==DialogResult.Yes)
10 {
11 ShouldSvae = true;
12 }
13 }
14 else
15 {
16 //如果用戶輸入了內容,並制定了一個文件名
17 if (textBox1.Text!=""&&saveFileDialog1.ShowDialog()==DialogResult.OK)
18 {
19 FileName = saveFileDialog1.FileName;
20 ShouldSvae = true;
21 }
22 }
23 if (ShouldSvae)
24 {
25 try
26 {
27 File.WriteAllText(FileName, textBox1.Text);
28 OriginalContent = textBox1.Text;
29 lbl_notice.Text = "文件已保存";
30 }
31 catch (Exception)
32 {
33 lbl_notice.Text = "文件保存失敗!!";
34 throw;
35 }
36 }
37 }
用了窗體的FormClosing事件。
1 //窗口關閉時保存文件
2 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
3 {
4 save();
5 }
(1)設置多行:multiline屬性;
(2)側拉框:Scrollbars屬性;
1 private void timer1_Tick(object sender, EventArgs e)
2 {
3 lblTimer.Text = DateTime.Now.ToString();
4 }
讓窗口標題顯示文件的名字。
1 //窗體初始值
2 private void Form1_Load(object sender, EventArgs e)
3 {
4 lblTimer.Text = "";
5 lbl_notice.Text = "";
6 Text = "無標題-我的記事本";
7
8 }
9 //窗體標題顯示文件名
10 private string OriginalContent;
11
12 private string _FileName = "";
13
14 public string FileName
15 {
16 get { return _FileName; }
17
18 set
19 {
20 //更新窗體標題
21 _FileName = value;
22 //path用System.IO引用
23 Text = Path.GetFileName(value) + "-我的記事本";
24
25 }
26 }

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.IO;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace Notepad
13 {
14 public partial class Form1 : Form
15 {
16 public Form1()
17 {
18 InitializeComponent();
19
20 }
21
22
23 private void timer1_Tick(object sender, EventArgs e)
24 {
25 lblTimer.Text = DateTime.Now.ToString();
26 }
27
28 //窗體初始值
29 private void Form1_Load(object sender, EventArgs e)
30 {
31 lblTimer.Text = "";
32 lbl_notice.Text = "";
33 Text = "無標題-我的記事本";
34
35 }
36 //窗體標題顯示文件名
37 private string OriginalContent;
38
39 private string _FileName = "";
40
41 public string FileName
42 {
43 get { return _FileName; }
44
45 set
46 {
47 //更新窗體標題
48 _FileName = value;
49 //path用System.IO引用
50 Text = Path.GetFileName(value) + "-我的記事本";
51
52 }
53 }
54
55 private void btn_open_Click(object sender, EventArgs e)
56 {
57 if (openFileDialog1.ShowDialog()==DialogResult.OK)
58 {
59 FileName = openFileDialog1.FileName;
60
61 try
62 {
63 OriginalContent = File.ReadAllText(FileName);
64 textBox1.Text = OriginalContent;
65 }
66 catch
67 {
68
69 lbl_notice.Text = "文件無法打開!";
70 }
71 }
72 }
73
74 private void btn_save_Click(object sender, EventArgs e)
75 {
76 save();
77 }
78
79 private void save()
80 {
81 //當內容已改變時,此標記為True
82 bool ShouldSvae = false;
83 //如果文件名不為空,表明當前時文本框中的內容是來自於文件
84 if (FileName!="")
85 {
86 //如果內容改變
87 if (textBox1.Text!=OriginalContent&&MessageBox.Show("文件已修改,是否保存?" , "保存文件" , MessageBoxButtons.YesNo)==DialogResult.Yes)
88 {
89 ShouldSvae = true;
90 }
91 }
92 else
93 {
94 //如果用戶輸入了內容,並制定了一個文件名
95 if (textBox1.Text!=""&&saveFileDialog1.ShowDialog()==DialogResult.OK)
96 {
97 FileName = saveFileDialog1.FileName;
98 ShouldSvae = true;
99 }
100 }
101 if (ShouldSvae)
102 {
103 try
104 {
105 File.WriteAllText(FileName, textBox1.Text);
106 OriginalContent = textBox1.Text;
107 lbl_notice.Text = "文件已保存";
108 }
109 catch (Exception)
110 {
111 lbl_notice.Text = "文件保存失敗!!";
112 throw;
113 }
114 }
115 }
116 //窗口關閉時保存文件
117 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
118 {
119 save();
120 }
121
122 }
123 }
View Code
通過winform實例的練習,發現可以自定義自己想要的功能了。