winform实例(1)-简单记事本

前言:利用这段时间学习C#的时间,简单做了一些winform窗体。

首先,先来一张效果图。

所需控件:

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         }

 

textbox控件的细节:

(1)设置多行:multiline属性;

(2)侧拉框:Scrollbars属性;

Timer控件:

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实例的练习,发现可以自定义自己想要的功能了。

 

posted @ 2016-10-25 11:05  智博的日常  阅读(5751)  评论(0编辑  收藏  举报