c#如何實(shí)現(xiàn)記事本

c#
小億
82
2024-09-05 04:57:24

在C#中,你可以使用Windows Forms或WPF(Windows Presentation Foundation)來(lái)創(chuàng)建一個(gè)簡(jiǎn)單的記事本應(yīng)用程序。以下是一個(gè)簡(jiǎn)單的Windows Forms記事本示例:

  1. 首先,打開(kāi)Visual Studio并創(chuàng)建一個(gè)新的Windows Forms應(yīng)用程序項(xiàng)目。

  2. 在解決方案資源管理器中,雙擊“Form1.cs”以打開(kāi)設(shè)計(jì)器。

  3. 從工具箱中,將以下控件添加到表單上:

    • 一個(gè)MenuStrip控件
    • 一個(gè)TextBox控件(用于顯示文本)
    • 一個(gè)StatusStrip控件
  4. 為MenuStrip控件添加以下菜單項(xiàng):

    • 文件(File)-> 新建(New)
    • 文件(File)-> 打開(kāi)(Open)
    • 文件(File)-> 保存(Save)
    • 文件(File)-> 退出(Exit)
    • 編輯(Edit)-> 剪切(Cut)
    • 編輯(Edit)-> 復(fù)制(Copy)
    • 編輯(Edit)-> 粘貼(Paste)
    • 編輯(Edit)-> 刪除(Delete)
    • 幫助(Help)-> 關(guān)于(About)
  5. 為這些菜單項(xiàng)添加Click事件處理程序。

  6. 在Form1.cs中,為每個(gè)菜單項(xiàng)的Click事件添加以下代碼:

using System;
using System.IO;
using System.Windows.Forms;

namespace Notepad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileContent = File.ReadAllText(openFileDialog.FileName);
                textBox1.Text = fileContent;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog.FileName, textBox1.Text);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Paste();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectedText = "";
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Simple Notepad application created in C#", "About");
        }
    }
}
  1. 運(yùn)行應(yīng)用程序,你將看到一個(gè)簡(jiǎn)單的記事本應(yīng)用程序,支持新建、打開(kāi)、保存、剪切、復(fù)制、粘貼和刪除等基本功能。

這只是一個(gè)簡(jiǎn)單的示例,你可以根據(jù)需要添加更多功能,例如字體選擇、顏色選擇、搜索和替換等。

0