主頁 > .NET開發 > C#撰寫一個較完整的記事本程式

C#撰寫一個較完整的記事本程式

2020-09-18 11:01:09 .NET開發

 

開發環境

Visual Studio 2019

  • 至少需安裝 .NET桌面開發

在這里插入圖片描述

創建專案并配置

創建表單檔案

在這里插入圖片描述

配置專案名稱及框架

在這里插入圖片描述

設計界面

創建表單檔案,將控制元件擺放位置如下,參考系統自帶的記事本程式
表單控制元件分布

表單添加的控制元件和組件如下

  • 控制元件及組件在工具箱查找

表單所需添加的控制元件和組件

表單屬性

表單屬性

快捷鍵設定

  • 雜項 --> ShortcutKeys

在這里插入圖片描述

程式屬性

專案屬性如下圖,在創建專案時就已定好了框架,如果在另一臺主機上的框架版本比目前專案框架版本低的話,則運行不起來

  • 文章末尾有整個程式的壓縮包鏈接可下載,如需直接運行則需下載對應的.NET Framework 4.7.2框架

專案屬性

程式圖示可在此設定,生成程式后的圖示如下圖,此檔案夾下的程式檔案可在第二臺主機上直接運行(專案\bin\Debug目錄下就是生成程式檔案的存放位置,雙擊程式檔案即可運行)

專案生成檔案的路徑

代碼演示

代碼開頭的using部分

  • 注釋部分需自行添加
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;
using System.Diagnostics;//提供了用于與事件日志、性能計數器和系統行程進行互動的類

主要功能

1.新建檔案:

    private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (txtBox.Modified == true)
        {
            DialogResult dr = MessageBox.Show("檔案發生變化,是否更改保存?", "注意", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                保存SToolStripMenuItem_Click(sender, e);
                return;
            }
            else if (dr == DialogResult.Cancel)
            {
                return;
            }
            txtBox.Clear();
            this.Text = "NewNotepad";
        }
        else
        {
            txtBox.Clear();
            this.Text = "NewNotepad";
        }
    }
新建檔案

2.打開:

    private void 打開ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            filename = openFileDialog.FileName;
            OpenFile();
        }
    }
    protected void OpenFile()
    {
        try
        {
            txtBox.Clear();
            txtBox.Text = File.ReadAllText(filename);
        }
        catch
        { MessageBox.Show("Error!"); }
    }
打開

3.保存:

    private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            StreamWriter sw = File.AppendText(Application.ExecutablePath);
            sw.Write(txtBox.Text);
            sw.Dispose();
        }
        catch
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.DefaultExt = "*.txt";
            sf.Filter = "文本檔案(.txt)|*.txt";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = File.AppendText(sf.FileName);
                sw.Write(txtBox.Text);
                sw.Dispose();
            }
        }
    }
保存

4.另存為:

    private void 另存為ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string name;
        //SaveFileDialog類
        SaveFileDialog save = new SaveFileDialog();
        //過濾器
        save.Filter = "*.txt|*.TXT|(*.*)|*.*";
        //顯示
        if (save.ShowDialog() == DialogResult.OK)
        {
            name = save.FileName;
            FileInfo info = new FileInfo(name);
            //info.Delete();
            StreamWriter writer = info.CreateText();
            writer.Write(txtBox.Text);
            writer.Close();
        }
    }
另存為

5.列印:

    private void 列印PToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //顯示允許用戶選擇列印機的選項及其它列印選項的對話框
        this.printDialog.Document = this.printDocument;
        this.printDialog.PrinterSettings = this.pageSetupDialog.PrinterSettings;
        //向列印機發送列印指令
        if (this.printDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this.printDocument.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "錯誤資訊!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
列印

6.編輯:

  • 根據輸入是否輸入內容控制是否啟用功能
    private void 編輯ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        剪切ToolStripMenuItem.Enabled = txtBox.Modified;
        if (txtBox.SelectedText == "")
        {
            剪切ToolStripMenuItem.Enabled = false;
            復制ToolStripMenuItem.Enabled = false;
            洗掉ToolStripMenuItem.Enabled = false;
        }
        else
        {
            剪切ToolStripMenuItem.Enabled = true;
            復制ToolStripMenuItem.Enabled = true;
            洗掉ToolStripMenuItem.Enabled = true;
        }
        if (txtBox.Text == "")
        {
            查找ToolStripMenuItem.Enabled = false;
            查找下一個ToolStripMenuItem.Enabled = false;
            查找上一個ToolStripMenuItem.Enabled = false;
            替換ToolStripMenuItem.Enabled = false;
        }
        else
        {
            查找ToolStripMenuItem.Enabled = true;
            查找下一個ToolStripMenuItem.Enabled = true;
            查找上一個ToolStripMenuItem.Enabled = true;
            替換ToolStripMenuItem.Enabled = true;
        }
        if (Clipboard.GetText() == "")
            粘貼ToolStripMenuItem.Enabled = false;
        else
            粘貼ToolStripMenuItem.Enabled = true;
    }
編輯

7.查找:

  • 查找功能不夠完善,混用查找上一項和查找下一項效果不理想
    TextBox txtInput = new TextBox()
    {
        Font = new Font("宋體", 10)
    };
    TextBox txtInputReplace = new TextBox()
    {
        Font = new Font("宋體", 10)
    };
    Label lblSearch = new Label
    {
        Text = "查找內容:",
        Size = new Size(65, 25),
        Location = new Point(5, 22)
    };
    Label lblDirection = new Label
    {
        Text = "查找方向:",
        Size = new Size(65, 25),
        Location = new Point(5, 58)
    };
    Button FindNext = new Button
    {
        Name = "btnFindNext",
        Text = "查找下一項",
        Size = new Size(80, 25),
        Location = new Point(265, 15)
    };
    Button Cancel = new Button
    {
        Name = "btnCancel",
        Text = "取消",
        Size = new Size(80, 25),
        Location = new Point(265, 50)
    };
    RadioButton down = new RadioButton
    {
        Name = "radDown",
        Text = "向下",
        Size = new Size(55, 25),
        Location = new Point(70, 53),
        Checked = true
    };
    RadioButton upward = new RadioButton
    {
        Name = "radUpward",
        Text = "向上",
        Size = new Size(55, 25),
        Location = new Point(140, 53),
        Checked = false
    };
    new Form FindForm = new Form
    {
        Text = "查找文本",
        FormBorderStyle = FormBorderStyle.FixedSingle,
        MaximizeBox = false,
        MinimizeBox = false
    };
    private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //顯示查找對話框
        txtInput.Size = new Size(190, 33);
        txtInput.Location = new Point(70, 15);
        txtInput.Multiline = true;

        FindNext.Click += new EventHandler(Direction_Click);
        //FindNext.Click += new EventHandler(Visible_Click);

        Cancel.Click += new EventHandler(Cancel_Click);

        FindForm.Controls.Add(lblSearch);
        FindForm.Controls.Add(lblDirection);
        FindForm.Controls.Add(txtInput);
        FindForm.Controls.Add(down);
        FindForm.Controls.Add(upward);
        FindForm.Controls.Add(FindNext);
        FindForm.Controls.Add(Cancel);
        FindForm.Top = this.Top + 50;
        FindForm.Left = this.Left + 50;
        FindForm.Height = 120;
        FindForm.Width = 380;
        FindForm.StartPosition = FormStartPosition.CenterParent;
        FindForm.ShowDialog();
    }
    private void Cancel_Click(object sender, EventArgs e)
    {
        //關閉對話框
        FindForm.Close();
        ReplaceForm.Close();
    }
    private void Direction_Click(object sender, EventArgs e)
    {
        //選擇字符查找方向
        if (down.Checked == true)
        {
            Find_Click(sender, e);
        }
        else if (upward.Checked == true)
        {
            FindLast_Click(sender, e);
        }
    }
            int nextPosition, firstPosition;
    string word;
    Boolean IF = false;
    private void Find_Click(object sender, EventArgs e)
    {
        txtBox.Focus();
        FindWords(txtInput.Text);
    }
    private void FindWords(string words)
    {
        //向下查找字符
        if (nextPosition >= txtBox.Text.Length)
            nextPosition = 0;
        firstPosition = txtBox.Text.IndexOf(words, nextPosition);
        if (firstPosition == -1)
            nextPosition = 0;
        else
        {
            txtBox.Select(firstPosition, words.Length);
            nextPosition = firstPosition + 1;
        }
        word = words;
        IF = true;
    }
查找

在這里插入圖片描述

8. 查找下一項 :

    private void 查找下一個ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //查找下一項,如果未查找過,則顯示查找對話框
        down.Checked = true;
        upward.Checked = false;
        try
        {
            FindWords(word);
        }
        catch
        {
            查找ToolStripMenuItem_Click(sender, e);
        }
    }
查找下一項

9.查找上一項:

    private void FindWordsLast(string words)
    {
        //向上查找字符
        if (IF == false)
            nextPosition = txtBox.Text.Length;
        if (nextPosition < 0)
            nextPosition = txtBox.Text.Length;

        firstPosition = txtBox.Text.LastIndexOf(words, nextPosition);

        if (firstPosition == -1)
            nextPosition = txtBox.Text.Length;
        else
        {
            txtBox.Select(firstPosition, words.Length);
            nextPosition = firstPosition - 1;
        }
        word = words;
        IF = true;
    }
    private void 查找上一個ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //查找上一項,如果未查找過,則顯示查找對話框
        upward.Checked = true;
        down.Checked = false;
        try
        {
            FindWordsLast(word);
        }
        catch
        {
            查找ToolStripMenuItem_Click(sender, e);
        }
    }
查找上一項

10.替換:

    Label LblReplace = new Label
    {
        Name = "lblReplace",
        Text = "替換:",
        Size = new Size(55, 25),
        Location = new Point(15, 50)
    };
    Form ReplaceForm = new Form
    {
        Text = "替換文本",
        FormBorderStyle = FormBorderStyle.FixedSingle,
        MaximizeBox = false,
        MinimizeBox = false
    };
    private void 替換ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        txtInput.Size = new Size(190, 30);
        txtInput.Location = new Point(70, 12);
        txtInput.Multiline = true;

        txtInputReplace.Size = new Size(190, 30);
        txtInputReplace.Location = new Point(70, 47);
        txtInputReplace.Multiline = true;

        Button Replace = new Button
        {
            Name = "btnReplace",
            Text = "替換",
            Size = new Size(80, 25),
            Location = new Point(265, 15)
        };
        Replace.Click += new EventHandler(Replace_Click);
        Cancel.Click += new EventHandler(Cancel_Click);

        ReplaceForm.Controls.Add(lblSearch);
        ReplaceForm.Controls.Add(LblReplace);
        ReplaceForm.Controls.Add(txtInput);
        ReplaceForm.Controls.Add(txtInputReplace);
        ReplaceForm.Controls.Add(Replace);
        ReplaceForm.Controls.Add(Cancel);
        ReplaceForm.Top = this.Top + 50;
        ReplaceForm.Left = this.Left + 50;
        ReplaceForm.Height = 140;
        ReplaceForm.Width = 380;
        ReplaceForm.StartPosition = FormStartPosition.CenterParent;
        ReplaceForm.ShowDialog();
    }
    private void Replace_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text.Replace(txtInput.Text, txtInputReplace.Text);
    }
替換

在這里插入圖片描述

11. 字體選擇:

  • 直接呼叫控制元件即可
    private void 字體ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //提示用戶從本地計算機安裝的字體中選擇字體字號
        FontDialog fontDialog = new FontDialog();
        if (fontDialog.ShowDialog() == DialogResult.OK)
        {
            txtBox.Font = fontDialog.Font;
        }
    }
字體選擇

在這里插入圖片描述

12. 關于記事本:

  • 新建一個視窗,根據自己的喜好添加標簽及擺放位置
    private void 關于記事本ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //關于記事本說明
        Label lblTitle = new Label()
        {
            Text = "多功能記事本",
            Size = new Size(150, 25),
            Location = new Point(100, 50)
        };
        Label lblEdition = new Label()
        {
            Text = "版本號:個性測驗版",
            Size = new Size(150, 25),
            Location = new Point(85, 100)
        };
        Label lblMail = new Label()
        {
            Text = "E-Mail:",
            Size = new Size(55, 25),
            Location = new Point(30, 180)
        };
        LinkLabel llblMail = new LinkLabel()
        {
            Text = "[email protected]",
            Size = new Size(110, 25),
            Location = new Point(85, 180)
        };
        Label lblCNDS = new Label()
        {
            Text = "CNDS博客:",
            Size = new Size(65, 25),
            Location = new Point(20, 220)
        };
        LinkLabel llblCNDS = new LinkLabel()
        {
            Text = "https://blog.csdn.net/UFO_Harold",
            Size = new Size(200, 25),
            Location = new Point(85, 220)
        };
        Form about = new Form
        {
            Text = "關于記事本",
            FormBorderStyle = FormBorderStyle.FixedSingle,
            MaximizeBox = false
        };

        llblCNDS.Click += new EventHandler(LlblCNDS_Click);
        about.Controls.Add(lblTitle);
        about.Controls.Add(lblEdition);
        about.Controls.Add(lblMail);
        about.Controls.Add(llblMail);
        about.Controls.Add(lblCNDS);
        about.Controls.Add(llblCNDS);
        about.Top = this.Top + this.Height / 2 - about.Height / 2;
        about.Left = this.Left + this.Width / 2 - about.Width / 2;
        about.StartPosition = FormStartPosition.CenterParent;
        about.ShowDialog();
    }
關于記事本
  • 效果如圖

程式的關于記事本功能展示

完整代碼

namespace Notepad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string filename = "";
        public Form1(string filename)
        {
            InitializeComponent();
            if (filename != null)
            {
                this.filename = filename;
                OpenFile();
            }
        }
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (txtBox.Modified == true)
            {
                DialogResult dr = MessageBox.Show("檔案發生變化,是否更改保存?", "注意", MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Yes)
                {
                    保存SToolStripMenuItem_Click(sender, e);
                    return;
                }
                else if (dr == DialogResult.Cancel)
                {
                    return;
                }
                txtBox.Clear();
                this.Text = "NewNotepad";
            }
            else
            {
                txtBox.Clear();
                this.Text = "NewNotepad";
            }
        }
        private void 打開ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openFileDialog.FileName;
                OpenFile();
            }
        }
        protected void OpenFile()
        {
            try
            {
                txtBox.Clear();
                txtBox.Text = File.ReadAllText(filename);
            }
            catch
            { MessageBox.Show("Error!"); }
        }
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                StreamWriter sw = File.AppendText(Application.ExecutablePath);
                sw.Write(txtBox.Text);
                sw.Dispose();
            }
            catch
            {
                SaveFileDialog sf = new SaveFileDialog();
                sf.DefaultExt = "*.txt";
                sf.Filter = "文本檔案(.txt)|*.txt";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter sw = File.AppendText(sf.FileName);
                    sw.Write(txtBox.Text);
                    sw.Dispose();
                }
            }
        }
        private void 另存為ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string name;
            //SaveFileDialog類
            SaveFileDialog save = new SaveFileDialog();
            //過濾器
            save.Filter = "*.txt|*.TXT|(*.*)|*.*";
            //顯示
            if (save.ShowDialog() == DialogResult.OK)
            {
                name = save.FileName;
                FileInfo info = new FileInfo(name);
                //info.Delete();
                StreamWriter writer = info.CreateText();
                writer.Write(txtBox.Text);
                writer.Close();
            }
        }
        private void 頁面設定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //彈出頁面設定界面
            pageSetupDialog.Document = printDocument;
            pageSetupDialog.ShowDialog();
        }
        private void 列印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //顯示允許用戶選擇列印機的選項及其它列印選項的對話框
            this.printDialog.Document = this.printDocument;
            this.printDialog.PrinterSettings = this.pageSetupDialog.PrinterSettings;
            //向列印機發送列印指令
            if (this.printDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.printDocument.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "錯誤資訊!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void 編輯ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            剪切ToolStripMenuItem.Enabled = txtBox.Modified;
            if (txtBox.SelectedText == "")
            {
                剪切ToolStripMenuItem.Enabled = false;
                復制ToolStripMenuItem.Enabled = false;
                洗掉ToolStripMenuItem.Enabled = false;
            }
            else
            {
                剪切ToolStripMenuItem.Enabled = true;
                復制ToolStripMenuItem.Enabled = true;
                洗掉ToolStripMenuItem.Enabled = true;
            }
            if (txtBox.Text == "")
            {
                查找ToolStripMenuItem.Enabled = false;
                查找下一個ToolStripMenuItem.Enabled = false;
                查找上一個ToolStripMenuItem.Enabled = false;
                替換ToolStripMenuItem.Enabled = false;
            }
            else
            {
                查找ToolStripMenuItem.Enabled = true;
                查找下一個ToolStripMenuItem.Enabled = true;
                查找上一個ToolStripMenuItem.Enabled = true;
                替換ToolStripMenuItem.Enabled = true;
            }
            if (Clipboard.GetText() == "")
                粘貼ToolStripMenuItem.Enabled = false;
            else
                粘貼ToolStripMenuItem.Enabled = true;
        }
        private void 撤銷ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (txtBox.CanUndo)
            {
                txtBox.Undo();
                txtBox.ClearUndo();
            }
        }
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBox.Cut();
        }
        private void 復制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBox.Copy();
        }
        private void 粘貼PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBox.Paste();
        }
        private void 洗掉lToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBox.SelectedText = string.Empty;
        }
        TextBox txtInput = new TextBox()
        {
            Font = new Font("宋體", 10)
        };
        TextBox txtInputReplace = new TextBox()
        {
            Font = new Font("宋體", 10)
        };
        Label lblSearch = new Label
        {
            Text = "查找內容:",
            Size = new Size(65, 25),
            Location = new Point(5, 22)
        };
        Label lblDirection = new Label
        {
            Text = "查找方向:",
            Size = new Size(65, 25),
            Location = new Point(5, 58)
        };
        Button FindNext = new Button
        {
            Name = "btnFindNext",
            Text = "查找下一項",
            Size = new Size(80, 25),
            Location = new Point(265, 15)
        };
        Button Cancel = new Button
        {
            Name = "btnCancel",
            Text = "取消",
            Size = new Size(80, 25),
            Location = new Point(265, 50)
        };
        RadioButton down = new RadioButton
        {
            Name = "radDown",
            Text = "向下",
            Size = new Size(55, 25),
            Location = new Point(70, 53),
            Checked = true
        };
        RadioButton upward = new RadioButton
        {
            Name = "radUpward",
            Text = "向上",
            Size = new Size(55, 25),
            Location = new Point(140, 53),
            Checked = false
        };
        new Form FindForm = new Form
        {
            Text = "查找文本",
            FormBorderStyle = FormBorderStyle.FixedSingle,
            MaximizeBox = false,
            MinimizeBox = false
        };
        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //顯示查找對話框
            txtInput.Size = new Size(190, 33);
            txtInput.Location = new Point(70, 15);
            txtInput.Multiline = true;

            FindNext.Click += new EventHandler(Direction_Click);
            //FindNext.Click += new EventHandler(Visible_Click);

            Cancel.Click += new EventHandler(Cancel_Click);

            FindForm.Controls.Add(lblSearch);
            FindForm.Controls.Add(lblDirection);
            FindForm.Controls.Add(txtInput);
            FindForm.Controls.Add(down);
            FindForm.Controls.Add(upward);
            FindForm.Controls.Add(FindNext);
            FindForm.Controls.Add(Cancel);
            FindForm.Top = this.Top + 50;
            FindForm.Left = this.Left + 50;
            FindForm.Height = 120;
            FindForm.Width = 380;
            FindForm.StartPosition = FormStartPosition.CenterParent;
            FindForm.ShowDialog();
        }
        private void Visible_Click(object sender, EventArgs e)
        {
            FindForm.Visible = false;
        }
        private void Cancel_Click(object sender, EventArgs e)
        {
            //關閉對話框
            FindForm.Close();
            ReplaceForm.Close();
        }
        private void Direction_Click(object sender, EventArgs e)
        {
            //選擇字符查找方向
            if (down.Checked == true)
            {
                Find_Click(sender, e);
            }
            else if (upward.Checked == true)
            {
                FindLast_Click(sender, e);
            }
        }
        int nextPosition, firstPosition;
        string word;
        Boolean IF = false;
        private void Find_Click(object sender, EventArgs e)
        {
            txtBox.Focus();
            FindWords(txtInput.Text);
        }
        private void FindWords(string words)
        {
            //向下查找字符
            if (nextPosition >= txtBox.Text.Length)
                nextPosition = 0;
            firstPosition = txtBox.Text.IndexOf(words, nextPosition);
            if (firstPosition == -1)
                nextPosition = 0;
            else
            {
                txtBox.Select(firstPosition, words.Length);
                nextPosition = firstPosition + 1;
            }
            word = words;
            IF = true;
        }
        private void 查找下一個ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //查找下一項,如果未查找過,則顯示查找對話框
            down.Checked = true;
            upward.Checked = false;
            try
            {
                FindWords(word);
            }
            catch
            {
                查找ToolStripMenuItem_Click(sender, e);
            }
        }
        private void FindLast_Click(object sender, EventArgs e)
        {
            txtBox.Focus();
            FindWordsLast(txtInput.Text);
        }
        private void FindWordsLast(string words)
        {
            //向上查找字符
            if (IF == false)
                nextPosition = txtBox.Text.Length;
            if (nextPosition < 0)
                nextPosition = txtBox.Text.Length;

            firstPosition = txtBox.Text.LastIndexOf(words, nextPosition);

            if (firstPosition == -1)
                nextPosition = txtBox.Text.Length;
            else
            {
                txtBox.Select(firstPosition, words.Length);
                nextPosition = firstPosition - 1;
            }
            word = words;
            IF = true;
        }
        private void 查找上一個ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //查找上一項,如果未查找過,則顯示查找對話框
            upward.Checked = true;
            down.Checked = false;
            try
            {
                FindWordsLast(word);
            }
            catch
            {
                查找ToolStripMenuItem_Click(sender, e);
            }
        }
        Label LblReplace = new Label
        {
            Name = "lblReplace",
            Text = "替換:",
            Size = new Size(55, 25),
            Location = new Point(15, 50)
        };
        Form ReplaceForm = new Form
        {
            Text = "替換文本",
            FormBorderStyle = FormBorderStyle.FixedSingle,
            MaximizeBox = false,
            MinimizeBox = false
        };
        private void 替換ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtInput.Size = new Size(190, 30);
            txtInput.Location = new Point(70, 12);
            txtInput.Multiline = true;

            txtInputReplace.Size = new Size(190, 30);
            txtInputReplace.Location = new Point(70, 47);
            txtInputReplace.Multiline = true;

            Button Replace = new Button
            {
                Name = "btnReplace",
                Text = "替換",
                Size = new Size(80, 25),
                Location = new Point(265, 15)
            };
            Replace.Click += new EventHandler(Replace_Click);
            Cancel.Click += new EventHandler(Cancel_Click);

            ReplaceForm.Controls.Add(lblSearch);
            ReplaceForm.Controls.Add(LblReplace);
            ReplaceForm.Controls.Add(txtInput);
            ReplaceForm.Controls.Add(txtInputReplace);
            ReplaceForm.Controls.Add(Replace);
            ReplaceForm.Controls.Add(Cancel);
            ReplaceForm.Top = this.Top + 50;
            ReplaceForm.Left = this.Left + 50;
            ReplaceForm.Height = 140;
            ReplaceForm.Width = 380;
            ReplaceForm.StartPosition = FormStartPosition.CenterParent;
            ReplaceForm.ShowDialog();
        }
        private void Replace_Click(object sender, EventArgs e)
        {
            txtBox.Text = txtBox.Text.Replace(txtInput.Text, txtInputReplace.Text);
        }
        private void 全選AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBox.SelectAll();
        }
        private void 自動換行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //默認自動換行,點擊按鈕打開或關閉自動換行
            if (自動換行ToolStripMenuItem.Checked == true)
            {
                txtBox.WordWrap = false;
                自動換行ToolStripMenuItem.Checked = false;
            }
            else
            {
                txtBox.WordWrap = true;
                自動換行ToolStripMenuItem.Checked = true;
            }
        }
        private void 字體ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //提示用戶從本地計算機安裝的字體中選擇字體字號
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                txtBox.Font = fontDialog.Font;
            }
        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //表單的txtBox控制元件隨表單改變而改變的大小
            if (狀態欄ToolStripMenuItem.Checked == true && 工具列TToolStripMenuItem.Checked == true)
                txtBox.Height = this.Height - menuStrip.Height - toolStrip.Height - statusStrip.Height - 39;
            else if (狀態欄ToolStripMenuItem.Checked == false && 工具列TToolStripMenuItem.Checked == true)
                txtBox.Height = this.Height - menuStrip.Height - toolStrip.Height - 39;
            else if (狀態欄ToolStripMenuItem.Checked == true && 工具列TToolStripMenuItem.Checked == false)
                txtBox.Height = this.Height - menuStrip.Height - statusStrip.Height - 39;
            else
                txtBox.Height = this.Height - menuStrip.Height - 39;
            txtBox.Width = this.Width - 16;
        }
        private void 工具列TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //默認打開工具列,點擊按鈕打開或關閉工具列
            if (工具列TToolStripMenuItem.Checked == true)
            {
                toolStrip.Visible = false;
                工具列TToolStripMenuItem.Checked = false;
                txtBox.Top = 25;
            }
            else if (工具列TToolStripMenuItem.Checked == false)
            {
                toolStrip.Visible = true;
                工具列TToolStripMenuItem.Checked = true;
                txtBox.Top = 50;
            }
            Form1_SizeChanged(sender, e);
        }
        private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //放大字體大小
            var fontsize = txtBox.Font.Size;
            var fontFamily = txtBox.Font.FontFamily;
            txtBox.Font = new Font(fontFamily, fontsize + 1);
        }
        private void 縮小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //縮小字體大小
            var fontsize = txtBox.Font.Size;
            var fontFamily = txtBox.Font.FontFamily;
            txtBox.Font = new Font(fontFamily, fontsize - 1);
        }
        private void 恢復默認縮放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //恢復默認字體大小
            txtBox.Font = new Font(txtBox.Font.FontFamily, 11);
        }
        private void 狀態欄ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //默認顯示狀態欄,點擊按鈕顯示或關閉狀態欄
            if (狀態欄ToolStripMenuItem.Checked == true)
            {
                statusStrip.Visible = false;
                狀態欄ToolStripMenuItem.Checked = false;
            }
            else if (狀態欄ToolStripMenuItem.Checked == false)
            {
                statusStrip.Visible = true;
                狀態欄ToolStripMenuItem.Checked = true;
            }
            Form1_SizeChanged(sender, e);
        }
        //private int GetStringLen(string s)
        //{
        //    if (!string.IsNullOrEmpty(s))
        //    {
        //        int len = s.Length;
        //        for (int i = 0; i < s.Length; i++)
        //        {
        //            if (s[i] > 255)
        //                len++;
        //        }
        //        return len;
        //    }
        //    return 0;
        //}
        private void 查看幫助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //呼叫系統自帶的瀏覽器打開網頁查看幫助
            Process.Start("https://jingyan.baidu.com/article/a24b33cdd86a0f19fe002be9.html");
        }
        private void 關于記事本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //關于記事本說明
            Label lblTitle = new Label()
            {
                Text = "多功能記事本",
                Size = new Size(150, 25),
                Location = new Point(100, 50)
            };
            Label lblEdition = new Label()
            {
                Text = "版本號:個性測驗版",
                Size = new Size(150, 25),
                Location = new Point(85, 100)
            };
            Label lblMail = new Label()
            {
                Text = "E-Mail:",
                Size = new Size(55, 25),
                Location = new Point(30, 180)
            };
            LinkLabel llblMail = new LinkLabel()
            {
                Text = "[email protected]",
                Size = new Size(110, 25),
                Location = new Point(85, 180)
            };
            Label lblCNDS = new Label()
            {
                Text = "CNDS博客:",
                Size = new Size(65, 25),
                Location = new Point(20, 220)
            };
            LinkLabel llblCNDS = new LinkLabel()
            {
                Text = "https://blog.csdn.net/UFO_Harold",
                Size = new Size(200, 25),
                Location = new Point(85, 220)
            };
            Form about = new Form
            {
                Text = "關于記事本",
                FormBorderStyle = FormBorderStyle.FixedSingle,
                MaximizeBox = false
            };

            llblCNDS.Click += new EventHandler(LlblCNDS_Click);
            about.Controls.Add(lblTitle);
            about.Controls.Add(lblEdition);
            about.Controls.Add(lblMail);
            about.Controls.Add(llblMail);
            about.Controls.Add(lblCNDS);
            about.Controls.Add(llblCNDS);
            about.Top = this.Top + this.Height / 2 - about.Height / 2;
            about.Left = this.Left + this.Width / 2 - about.Width / 2;
            about.StartPosition = FormStartPosition.CenterParent;
            about.ShowDialog();
        }
        private void LlblCNDS_Click(object sender, EventArgs e)
        {
            Process.Start("https://blog.csdn.net/UFO_Harold");
        }
        private void 新建toolStripButton_Click(object sender, EventArgs e)
        {
            新建NToolStripMenuItem_Click(this, e);
        }
        private void 另存為toolStripButton_Click(object sender, EventArgs e)
        {
            另存為ToolStripMenuItem_Click(this, e);
        }
        private void 保存StoolStripButton_Click(object sender, EventArgs e)
        {
            保存SToolStripMenuItem_Click(this, e);
        }
        private void 列印PtoolStripButton_Click(object sender, EventArgs e)
        {
            列印PToolStripMenuItem_Click(this, e);
        }
        private void 剪切toolStripButton_Click(object sender, EventArgs e)
        {
            剪切ToolStripMenuItem_Click(this, e);
        }
        private void 復制CtoolStripButton_Click(object sender, EventArgs e)
        {
            復制CToolStripMenuItem_Click(this, e);
        }
        private void 粘貼PtoolStripButton_Click(object sender, EventArgs e)
        {
            粘貼PToolStripMenuItem_Click(this, e);
        }
        private void 幫助HtoolStripButton_Click(object sender, EventArgs e)
        {
            查看幫助HToolStripMenuItem_Click(this, e);
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            //顯示編輯游標所在幾行幾列
            int row = txtBox.GetLineFromCharIndex(txtBox.SelectionStart) + 1;
            int col = (txtBox.SelectionStart - txtBox.GetFirstCharIndexFromLine(txtBox.GetLineFromCharIndex(txtBox.SelectionStart))) + 1;
            toolStripStatusLblLocation.Text = "第 " + row + " 行, 第 " + col + " 列";
            toolStripStatusLblNow.Text = "" + DateTime.Now.ToLocalTime();
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //關閉表單時如果已修改內容,則彈出是否保存對話框,否則直接關閉表單
            if (txtBox.Modified == true)
            {
                DialogResult dr = MessageBox.Show("檔案發生變化,是否更改保存?", "注意", MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Yes)
                {
                    保存SToolStripMenuItem_Click(sender, e);
                    return;
                }
                else if (dr == DialogResult.No)
                {
                    return;
                }
                else if (dr == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }
    }
}

運行結果

win10環境下運行程式的結果

注:

  1. 控制元件請自行改名,也可使用默認控制元件名,此次程式的控制元件均已自定義名稱,然后再雙擊控制元件便會自動創建控制元件的事件函式并跳到代碼頁,全數copy代碼到自己新建的程式可能運行不起來,因為控制元件的事件需要雙擊控制元件才跳轉到事件函式,事件方法前出現參考不是為 0 即生效;
  2. 查找上一項下一項功能混用時會有一些bug,達不到預期效果,但能運行,不會報錯,一點邏輯上的問題,目前沒有想到解決方法,大家可自行深入摸索,如有可以改進的地方可聯系博主;
  3. 整個專案原始碼的檔案:(原始碼僅供學習交流使用,如需使用請安裝.NET Framework 4.7.2框架,且圖示可能因檔案路徑不同而無法顯示,修改檔案路徑即可)
  • 藍奏云:https://www.lanzous.com/i9r643e
  • 百度網盤:https://pan.baidu.com/s/1BagLHS9bOG2jvaOcHgeUgA 提取碼:y639
  • Github:https://github.com/Harold-666/Notepad/tree/master

該文是從CSND搬家過來的文章,已修正,覺得CSND不好用,搬至博客園在此安家,總的來說,在博客園的體驗感比在CSDN好很多,往后請各位博友多多指教!我的博客園地址:https://www.cnblogs.com/Harold-popo

  • 狀態欄圖示設定

在這里插入圖片描述

  • 專案檔案目錄

在這里插入圖片描述

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/73205.html

標籤:C#

上一篇:ASP.NET Core 靜態類中如何使用快取

下一篇:細說列舉

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more