记事本实例

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7using System.IO;
  8
  9namespace NotePad
 10{
 11    /// <summary>
 12    /// 记事本实例。
 13    /// </summary>

 14    public class Form1 : System.Windows.Forms.Form
 15    {
 16        private System.Windows.Forms.MainMenu mainMenu1;
 17        private System.Windows.Forms.MenuItem menuItem1;
 18        private System.Windows.Forms.MenuItem menuItem3;
 19        private System.Windows.Forms.MenuItem menuItem4;
 20        private System.Windows.Forms.MenuItem menuItem5;
 21        private System.Windows.Forms.MenuItem menuItem6;
 22        private System.Windows.Forms.MenuItem menuItem7;
 23        private System.Windows.Forms.MenuItem menuItem8;
 24        private System.Windows.Forms.MenuItem menuItem9;
 25        private System.Windows.Forms.MenuItem menuItem10;
 26        private System.Windows.Forms.MenuItem menuItem14;
 27        private System.Windows.Forms.MenuItem menuItem15;
 28        private System.Windows.Forms.MenuItem menuItem16;
 29        private System.Windows.Forms.RichTextBox richTextBox1;
 30        private System.Windows.Forms.FontDialog fontDialog1;
 31        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
 32        private System.Windows.Forms.OpenFileDialog openFileDialog1;
 33        /// <summary>
 34        /// 必需的设计器变量。
 35        /// </summary>

 36        private System.ComponentModel.Container components = null;
 37
 38        public Form1()
 39        {
 40            //
 41            // Windows 窗体设计器支持所必需的
 42            //
 43            InitializeComponent();
 44
 45            //
 46            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 47            //
 48        }

 49
 50        /// <summary>
 51        /// 清理所有正在使用的资源。
 52        /// </summary>

 53        protected override void Dispose( bool disposing )
 54        {
 55            if( disposing )
 56            {
 57                if (components != null
 58                {
 59                    components.Dispose();
 60                }

 61            }

 62            base.Dispose( disposing );
 63        }

 64
 65        Windows Form Designer generated code
211
212        /// <summary>
213        /// 应用程序的主入口点。
214        /// </summary>

215        [STAThread]
216        static void Main() 
217        {
218            Application.Run(new Form1());
219        }

220
221
222        /// 定义程序中的私有变量。
223        private bool dirty = false;
224        private string filepath = "";
225
226        private void MySaving()
227        {
228            if (dirty)
229            {
230                if (MessageBox.Show("需要保存文件吗?","提示框",MessageBoxButtons.YesNo) == DialogResult.Yes)
231                {
232                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
233                    {
234                        dirty = false;
235                        if (filepath.Length == 0)
236                        {
237                            filepath = saveFileDialog1.FileName;
238                        }

239                        StreamWriter sTmp = new StreamWriter(filepath);
240                        sTmp.Write(richTextBox1.Text);
241                        sTmp.Flush();
242                        sTmp.Close();
243                    }

244                }

245            }

246        }

247        private void menuItem14_Click(object sender, System.EventArgs e)
248        {
249            MySaving();
250            if (dirty == false)
251                Close();
252        }

253        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
254        {
255            MySaving();
256            if (dirty == false)
257                e.Cancel = false;
258        }

259        private void menuItem15_Click(object sender, System.EventArgs e)
260        {
261            if (richTextBox1.WordWrap == true)
262            {
263                richTextBox1.WordWrap = false;
264                mainMenu1.MenuItems[2].MenuItems[0].Checked = false;
265            }

266            else
267            {
268                richTextBox1.WordWrap = true;
269                mainMenu1.MenuItems[2].MenuItems[0].Checked = true;
270            }

271        }

272        private void menuItem16_Click(object sender, System.EventArgs e)
273        {
274            if (fontDialog1.ShowDialog() == DialogResult.OK)
275            {
276                richTextBox1.Font = fontDialog1.Font;
277            }

278        }

279        private void menuItem5_Click(object sender, System.EventArgs e)
280        {
281            MessageBox.Show("记事本实例 1.0版""关于记事本");
282        }

283        private void richTextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
284        {
285            dirty = true;
286        }

287        private void menuItem6_Click(object sender, System.EventArgs e)
288        {
289            MySaving();
290            dirty = false;
291            richTextBox1.Text = "";
292        }

293        private void menuItem7_Click(object sender, System.EventArgs e)
294        {
295            MySaving();
296            if (openFileDialog1.ShowDialog() == DialogResult.OK)
297            {
298                dirty = false;
299                StreamReader sTmp = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.ASCII);
300                filepath = openFileDialog1.FileName;
301                richTextBox1.Text = "";
302                richTextBox1.Text = sTmp.ReadToEnd();
303            }

304        }

305        private void menuItem8_Click(object sender, System.EventArgs e)
306        {
307            MySaving();
308        }

309        private void menuItem9_Click(object sender, System.EventArgs e)
310        {
311            MySaving();
312        }

313    }

314}

315
posted on 2007-08-21 14:58  Gofficer  阅读(280)  评论(0)    收藏  举报