1. 程式人生 > >簡易記事本開發(第二次C#作業)

簡易記事本開發(第二次C#作業)

用C#語言開發記事本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 簡易記事本開發
{
    public partial class frmEditor : Form
    {
        public frmEditor()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 儲存原始內容
        /// </summary>
        private String Originalcontent = "";
        /// <summary>
        /// 開啟的檔名
        /// </summary>
        private String _FileName = "";
        public String FileName
        {
            get
            {
                return _FileName;
            }
            set
            {
                _FileName =value;
                //在賦值時同步更新窗體標題
                Text = Path.GetFileName(value) + "我的記事本";
            }
        }
        private void lblTimer_Click(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblTimer.Text = DateTime.Now.ToString();
        }

        private void frmEditor_Load(object sender, EventArgs e)
        {
            lblTimer.Text = " ";
            lblinfo.Text = " ";
            Text = "無標題——我的記事本";
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog1.FileName;
                try
                {
                    Originalcontent = File.ReadAllText(FileName);
                    txtEditor.Text = Originalcontent;
                }
                catch (Exception)
                {
                    lblinfo.Text = "無法開啟檔案";
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            /// <summary>
            /// 儲存到檔案
            /// </summary>
            //當內容已更改時,此標記為true
            bool ShouldSave = false;
            //如果檔名不為空,表明當前文字框中的內容來自於檔案
            if (FileName != "")
            {
                //如果此內容已經更改
                if (txtEditor.Text != Originalcontent
                    && MessageBox.Show("檔案已修改,儲存嗎?", "儲存檔案", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ShouldSave = true;
                }
            }
            else 
            {
                //如果使用者輸入了內容,並且指定一個檔名
                if (txtEditor.Text != "" && saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    FileName = saveFileDialog1.FileName;
                    ShouldSave = true;
                }
            }
            if (ShouldSave)
            {
                try
                {
                    File.WriteAllText(FileName, txtEditor.Text);
                    Originalcontent = txtEditor.Text;
                    lblinfo.Text = "檔案已儲存";
                }
                catch (Exception)
                {
                    lblinfo.Text = "儲存檔案失敗";
                }
            }

        }
    }
}
執行結果如下所示:在文字框中編輯內容,並儲存

下圖是開啟對以儲存文件,進行二次編輯並儲存

這次作業,我是用了一天才做完的。本來我以為會很簡單,都是視訊裡的內容,但我沒想到原來做一個軟體開發有那麼多的步驟,有那麼多的知識點要學習。通過這次記事本開發實戰,我對之前唐大仕老師講的知識有更深的理解,更能串起來連成一個知識面,而不是之前散亂的點!