使用C#寫出一個簡單的記事本程式

Hades_Dev發表於2016-06-07

上週老師下課的時候說到,寫出來記事本的同學可以期末加分,於是乎,就用兩個晚上的時間寫出了這個簡單的記事本程式。

程式語言: C#
程式設計環境: Visual Studio 2013
執行環境: .NET Framework 4.5

預覽:

預覽

功能:

標題欄
顯示檔案標題

選單欄
各類選單命令

檔案
- 新建
- 開啟
- 儲存
- 另存為
- 頁面設定
- 列印
- 退出

編輯
- 撤銷
- 剪下
- 複製
- 貼上
- 全選
- 時間/日期

格式
- 自動換行
- 字型

檢視
- 狀態列
- 工具欄
- 全屏模式

幫助
- 開源許可
- 檢視幫助
- 關於

工具欄
常用工具集合

標籤欄
檔案標籤顯示

工作區
編輯區

狀態列
顯示檔案狀態

  • 文字狀態(新建/已修改)
  • 字元個數
  • 游標座標

功能實現

本程式有兩個窗體,分別為Form1和AboutBox1

所有檔案如下:

這裡寫圖片描述

對於Form1:
需要引入的類庫:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

窗體及空間宣告程式碼:

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem 檔案ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 新建ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 開啟ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 儲存ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 另存為ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 編輯ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 關於ToolStripMenuItem;
        private System.Windows.Forms.TextBox editBox1;
        private System.Windows.Forms.ToolStripMenuItem 撤銷ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 剪下ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 複製ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 貼上ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 刪除ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 全選AToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 日期DToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem1;
        private System.Windows.Forms.ToolStripMenuItem 字型ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 檢視VToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 狀態列ToolStripMenuItem;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
        private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3;
        private System.Windows.Forms.ToolStripSeparator toolStripMenuItem4;
        private System.Windows.Forms.ToolStripMenuItem 頁面設定UToolStripMenuItem;
        private System.Drawing.Printing.PrintDocument printDocument1;
        private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
        private System.Windows.Forms.ToolStripMenuItem 列印PToolStripMenuItem;
        private System.Windows.Forms.PrintDialog printDialog1;
        private System.Windows.Forms.ToolStripMenuItem 檢視幫助HToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator toolStripMenuItem5;
        private System.Windows.Forms.ToolStripMenuItem 自動換行ToolStripMenuItem;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
        public System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel3;
        private System.Windows.Forms.ToolStripMenuItem 全屏模式ToolStripMenuItem;
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripButton newButton;
        private System.Windows.Forms.ToolStripButton openButton;
        private System.Windows.Forms.ToolStripButton saveButton;
        private System.Windows.Forms.ToolStripButton saveAsButton;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private System.Windows.Forms.ToolStripButton cutButton;
        private System.Windows.Forms.ToolStripButton copyButton;
        private System.Windows.Forms.ToolStripButton pasteButton;
        private System.Windows.Forms.ToolStripButton deleteButton;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private System.Windows.Forms.ToolStripButton timeButton;
        private System.Windows.Forms.ToolStripButton fullButton;
        private System.Windows.Forms.ToolStripButton textButton;
        private System.Windows.Forms.ToolStripMenuItem 工具欄ToolStripMenuItem;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel4;
        private System.Windows.Forms.ToolStripMenuItem 開源許可OToolStripMenuItem;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TextBox editBox2;

窗體載入:

        private void Form1_Load(object sender, EventArgs e)
        {
            this.editBox1.ScrollBars = ScrollBars.Both;
            this.editBox2.ScrollBars = ScrollBars.Both;
            this.Text = textFileName + " - " + programeName;//顯示檔名
            this.timer1.Start();
            editBox1.Focus();

        }

首先進行一些固定變數宣告:

        private string textFileName = "無標題";
        private string programeName = "Icey";
        private string filePath = "";
        private string asFilePath = "";
        private string selecteText = "";
        private string helpUrl = "https://blog.mayuko.cn/icey";
        private string openSourceUrl = "https://github.com/mayuko2012/icey";
        private string wrongMessage = "你好像遇到了錯誤...";
        private string fileFormat = "文字檔案(*.txt)|*.txt|Icey檔案(*.ice)|*.ice|C++檔案(*.cpp)|*.cpp|C檔案(*.c)|*.c|所有檔案(*.*)|(*.*)";
        private string tabFileName1 = "無標題1 - Icey";
        private string tabFileName2 = "無標題2 - Icey";
        Boolean saveFileStatus1 = false;
        Boolean textChanged1 = false;
        Boolean saveFileStatus2 = false;
        Boolean textChanged2 = false;

儲存檔案採用函式形式:

        private void saveFile()//儲存
        {
            if (!textFileName.Equals(""))
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Filter = fileFormat;
                saveFile.FileName = "*.txt";
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    filePath = saveFile.FileName;
                    StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default);
                    sw.Write((editBox1.Focused) ? editBox1.Text : editBox2.Text);
                    sw.Close();
                    if (editBox1.Focused)
                    {
                        tabFileName1 = saveFile.FileName + " - " + programeName;
                        saveFileStatus1 = true;
                    }
                    else if (editBox2.Focused)
                    {
                        tabFileName2 = saveFile.FileName + " - " + programeName;
                        saveFileStatus2 = true;
                    }

                }
            }
            toolStripStatusLabel4.Text = "已儲存";
        }

另存為採用函式形式:

        private void saveAsFile()//另存為
        {
            SaveFileDialog saveAsFile = new SaveFileDialog();
            saveAsFile.Filter = fileFormat;
            saveAsFile.FileName = "*.txt";
            if (saveAsFile.ShowDialog() == DialogResult.OK)
            {
                asFilePath = saveAsFile.FileName;
                StreamWriter sw = new StreamWriter(asFilePath, false, Encoding.Default);
                sw.WriteLine((editBox1.Focused) ? editBox1.Text : editBox2.Text);
                sw.Close();
                FileInfo fileInfo = new FileInfo(saveAsFile.FileName);
                textFileName = fileInfo.Name;
            }
            toolStripStatusLabel4.Text = "已儲存";
        }

新建函式:

         private void newFile()//新建
        {
            if (editBox1.Focused)
            {

                if (editBox1.Text != String.Empty && saveFileStatus1 == false && textChanged1 == true)//如果文字框不為空
                {
                    DialogResult result = MessageBox.Show("是否將視窗1已編輯檔案儲存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.Yes)
                    {
                        saveFile();
                        Application.Exit();
                    }
                    else if (result == DialogResult.No)
                    {
                        editBox1.Text = "";
                    }
                }
                else
                    editBox1.Text = "";
            }
            else if (editBox2.Focused)
            {
                if (editBox2.Text != String.Empty && saveFileStatus2 == false && textChanged2 == true)//如果文字框不為空
                {
                    DialogResult result = MessageBox.Show("是否將視窗2已編輯檔案儲存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.Yes)
                    {
                        saveFile();
                        Application.Exit();
                    }
                    else if (result == DialogResult.No)
                    {
                        editBox2.Text = "";
                    }
                }
                else
                    editBox2.Text = "";
            }
        }

開啟檔案函式:

        private void openFile()//開啟
         {
             OpenFileDialog openFile = new OpenFileDialog();
             openFile.Filter = fileFormat;
             if (openFile.ShowDialog() == DialogResult.OK)
             {
                 StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default);
                 if (editBox1.Focused)
                 {
                     editBox1.Text = sr.ReadToEnd();
                 }
                 else if(editBox2.Focused)
                 {
                     editBox2.Text = sr.ReadToEnd();
                 }
                 sr.Close();
                 FileInfo fileInfo = new FileInfo(openFile.FileName);
                 if (editBox1.Focused)
                 {
                     tabFileName1 = fileInfo.Name + " - " + programeName;
                     saveFileStatus1 = true;
                 }
                 else if (editBox2.Focused)
                 {
                     tabFileName2 = fileInfo.Name + " - " + programeName;
                     saveFileStatus2 = true;
                 }
                 textFileName = fileInfo.Name;
             }
         }

全屏模式函式:

        private void  fullScreen()//全屏
        {
            if (全屏模式ToolStripMenuItem.Checked == false)
            {
                this.WindowState = FormWindowState.Maximized;
                全屏模式ToolStripMenuItem.Checked = true;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            }
            else
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Normal;
                全屏模式ToolStripMenuItem.Checked = false;
            }
        }

退出選單命令:

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Text != String.Empty || saveFileStatus1 == false && textChanged1 == true)
            {
                this.tabPage1.Show();
                this.editBox1.Focus();
                DialogResult result = MessageBox.Show("是否將視窗1已編輯檔案儲存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    saveFile();
                    Application.Exit();
                }
                else if (result == DialogResult.No)
                {
                    Application.Exit();
                }
            }
            else if (editBox2.Text != String.Empty || saveFileStatus2 == false && textChanged2 == true)
            {
                this.tabPage2.Show();
                this.editBox2.Focus();
                DialogResult result = MessageBox.Show("是否將視窗2已編輯檔案儲存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    saveFile();
                    Application.Exit();
                }
                else if (result == DialogResult.No)
                {
                    Application.Exit();
                }
            }
            else
                Application.Exit();

        }

Bool變數,用於判斷TextBox是否發生變化:

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textChanged2 = true;
            toolStripStatusLabel4.Text = "已修改";
        }
        private void editBox2_TextChanged(object sender, EventArgs e)
        {
            textChanged2 = true;
            toolStripStatusLabel4.Text = "已修改";
        }

新建選單命令:

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newFile();
        }

開啟選單命令:

        private void 開啟ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFile();
        }

字型選單命令:

        private void 字型ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                if (editBox1.Focused)
                {
                    editBox1.Font = fontDialog.Font;
                }
                else
                    editBox2.Font = fontDialog.Font;
            }
        }

退出動作(當使用者點選窗體右上角退出按鈕時執行此操作):

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (editBox1.Text != String.Empty && e.CloseReason == CloseReason.UserClosing || saveFileStatus1 == false && textChanged1 == true)//如果文字框不為空&&觸發關閉按鈕事件
            {
                this.tabPage1.Show();
                this.editBox1.Focus();
                DialogResult result = MessageBox.Show("是否將窗體1已編輯檔案儲存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    saveFile();
                    e.Cancel = false;
                }
                else if (result == DialogResult.No)
                {
                    e.Cancel = false;
                }
                else if (result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
            else if (editBox2.Text != String.Empty && e.CloseReason == CloseReason.UserClosing || saveFileStatus2 == false && textChanged2 == true)//如果文字框不為空&&觸發關閉按鈕事件
            {
                this.tabPage2.Show();
                this.editBox2.Focus();
                DialogResult result = MessageBox.Show("是否將視窗2已編輯檔案儲存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    saveFile();
                    e.Cancel = false;
                }
                else if (result == DialogResult.No)
                {
                    e.Cancel = false;
                }
                else if (result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
            else
                Application.Exit();
        }

狀態列命令(狀態列是否顯示):

        private void 狀態列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (狀態列ToolStripMenuItem.Checked == true)
            {
                狀態列ToolStripMenuItem.Checked = false;
                statusStrip1.Visible = false;
            }
            else
            {
                狀態列ToolStripMenuItem.Checked = true;
                statusStrip1.Visible = true;
            }
        }

編輯命令:

        private void 編輯ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((editBox1.SelectedText.Equals("")))
            {
                剪下ToolStripMenuItem.Enabled = false;
                複製ToolStripMenuItem.Enabled = false;
                刪除ToolStripMenuItem.Enabled = false;
            }
            else
            {
                剪下ToolStripMenuItem.Enabled = true;
                複製ToolStripMenuItem.Enabled = true;
                刪除ToolStripMenuItem.Enabled = true;
            }

        }

全選命令:

        private void 全選AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.SelectAll();
            }
            else
                this.editBox2.SelectAll();
        }

剪下 複製 貼上 刪除命令:

        private void 剪下ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                selecteText = editBox1.SelectedText;
                this.editBox1.Cut();
            }
            else
            {
                selecteText = editBox2.SelectedText;
                this.editBox2.Cut();
            }
        }

        private void 撤銷ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.Undo();
            }
            else
                this.editBox2.Undo();
        }

        private void 複製ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.Copy();
            }
            else
                this.editBox2.Copy();
        }

        private void 貼上ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.Paste();
            }
            else
                this.editBox2.Paste();
        }

        private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.SelectedText = "";
            }
            else
                this.editBox2.SelectedText = "";
        }

儲存命令:

        private void 儲存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFile();
        }

另存為命令:

        private void 另存為ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveAsFile();
        }

時間戳命令:

        private void 日期DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                editBox1.AppendText(System.DateTime.Now.ToString());
            }
            else
                editBox2.AppendText(System.DateTime.Now.ToString());
        }

頁面設定命令:

        private void 頁面設定UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1;
            this.pageSetupDialog1.AllowMargins = true;
            this.pageSetupDialog1.AllowOrientation = true;
            this.pageSetupDialog1.AllowPaper = true;
            this.pageSetupDialog1.AllowPrinter = true;
            this.pageSetupDialog1.Document = this.printDocument1;
            pageSetupDialog1.ShowDialog();
        }

列印命令:

        private void 列印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.printDialog1.Document = this.printDocument1;
            this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings;
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.printDocument1.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, wrongMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

檢視幫助 關於命令:

        private void 檢視幫助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(helpUrl);  
        }

        private void 關於ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 about = new AboutBox1();
            about.StartPosition = FormStartPosition.CenterScreen; 
            about.Show();
            about.Owner = this;
            //timer1.Stop();
        }

自動換行命令:

        private void 自動換行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自動換行ToolStripMenuItem.Checked == true)
            {
                editBox1.WordWrap = false;
                editBox2.WordWrap = false;
                自動換行ToolStripMenuItem.Checked = false;
            }
            else
            {
                editBox1.WordWrap = true;
                editBox2.WordWrap = true;
                自動換行ToolStripMenuItem.Checked = true;
            }
        }

計時器(100 ms重新整理頻率):

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = (editBox1.Focused) ? editBox1.Text.Length.ToString() + " 個字元" : editBox2.Text.Length.ToString() + " 個字元";
            int totalline = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(editBox1.Text.Length) + 1 : editBox2.GetLineFromCharIndex(editBox2.Text.Length) + 1;//得到總行數
            int index = (editBox1.Focused) ? editBox1.GetFirstCharIndexOfCurrentLine() : editBox2.GetFirstCharIndexOfCurrentLine();//得到當前行第一個字元的索引
            int line = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(index) + 1 : editBox2.GetLineFromCharIndex(index) + 1;//得到當前行的行號
            int col = (editBox1.Focused) ? editBox1.SelectionStart - index + 1 : editBox2.SelectionStart - index + 1;//.SelectionStart得到游標所在位置的索引 - 當前行第一個字元的索引 = 游標所在的列數
            toolStripStatusLabel2.Text = "第" + line + "行,第" + col + "列";
            if (( (editBox1.Focused) ? editBox1.SelectedText.Equals(""):editBox2.SelectedText.Equals("")))
            {
                cutButton.Enabled = false;
                copyButton.Enabled = false;
                deleteButton.Enabled = false;
            }
            else
            {
                cutButton.Enabled = true;
                copyButton.Enabled = true;
                deleteButton.Enabled = true;
            }
            if (editBox1.Focused)
            {
                editBox1.Focus();
                this.Text = tabFileName1;
            }
            else
            {
                editBox2.Focus();
                this.Text = tabFileName2;
            }
            if (editBox2.Focused)
            {
                editBox2.Focus();
                this.Text = tabFileName2;
            }
            else
            {
                editBox1.Focus();
                this.Text = tabFileName1;
            }
        }

工具欄命令(工具欄是否顯示):

        private void 工具欄ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (工具欄ToolStripMenuItem.Checked == false)
            {
                toolStrip1.Visible = true;
                工具欄ToolStripMenuItem.Checked = true;
            }
            else if(工具欄ToolStripMenuItem.Checked == true)
            {
                toolStrip1.Visible = false;
                工具欄ToolStripMenuItem.Checked = false;
            }
        }

開源許可命令:

        private void 開源許可OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(openSourceUrl);  
        }

工具欄的各個按鈕:

        private void newButton_Click(object sender, EventArgs e)
        {
            newFile();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            openFile();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            saveFile();
        }

        private void saveAsButton_Click(object sender, EventArgs e)
        {
            saveAsFile();
        }

        private void cutButton_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                selecteText = editBox1.SelectedText;
                this.editBox1.Cut();
            }
            else
            {
                selecteText = editBox2.SelectedText;
                this.editBox2.Cut();
            }
        }

        private void copyButton_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.Copy();
            }
            else
                this.editBox2.Copy();
        }

        private void pasteButton_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.Paste();
            }
            else
                this.editBox2.Paste();
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                this.editBox1.SelectedText = "";
            }
            else
                this.editBox2.SelectedText = "";
        }

        private void timeButton_Click(object sender, EventArgs e)
        {
            if (editBox1.Focused)
            {
                editBox1.AppendText(System.DateTime.Now.ToString());
            }
            else
                editBox2.AppendText(System.DateTime.Now.ToString());
        }

        private void textButton_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                if (editBox1.Focused)
                {
                    editBox1.Font = fontDialog.Font;
                }
                else
                    editBox2.Font = fontDialog.Font;
            }
        }

        private void fullButton_Click(object sender, EventArgs e)
        {
            fullScreen();   
        }

標籤欄:

        private void tabPage1_Click(object sender, EventArgs e)
        {
            editBox1.Focus();
            this.Text = tabPage1.Text;
            if (textChanged1 == false)
            {
                toolStripStatusLabel4.Text = "新建";
            }
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
            editBox2.Focus();
            this.Text = tabPage2.Text;
            if (textChanged2 == false)
            {
                toolStripStatusLabel4.Text = "新建";
            }
        }

對於AboutBox:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
    partial class AboutBox1 : Form
    {
        public AboutBox1()
        {
            InitializeComponent();
            this.Text = String.Format("關於 {0}", AssemblyTitle);
            this.labelProductName.Text = AssemblyProduct;
            this.labelVersion.Text = String.Format("版本 {0}", AssemblyVersion);
            this.labelCopyright.Text = AssemblyCopyright;
            this.labelCompanyName.Text = AssemblyCompany;
            this.textBoxDescription.Text = AssemblyDescription;
        }

        #region 程式集特性訪問器

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        private void AboutBox1_Load(object sender, EventArgs e)
        {

        }

        private void okButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("已是最新版本!", "檢查更新");
        }
        private void AboutBox1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
            frm1.timer1.Start();
        }
    }
}

相關文章