C#简易记事本的实现

Posted 敲代码两年半的练习生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#简易记事本的实现相关的知识,希望对你有一定的参考价值。

【C#】简易记事本的实现

1 题目描述

试编写Windows应用程序,完成下列要求:
(1)Form1窗体设计界面如下,该程序功能:简易记事本。

在这里插入图片描述

(2)主菜单:文件 { 打开、保存 }、编辑 { 剪切、复制、粘贴 }、格式 { 字体、颜色 }、退出;
(3)工具栏:打开、保存、剪切、复制、粘贴、字体(label+comboBox)、颜色(label+comboBox);
(4)可实现以上菜单项和快捷工具的功能;

2 源码详解

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

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

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String text = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                using (StreamReader sr = new StreamReader(file))
                {
                    richTextBox1.LoadFile(file, RichTextBoxStreamType.PlainText);
                }
                richTextBox1.Text = text;
            }
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != "")
            {
                Clipboard.SetDataObject(richTextBox1.Text);
            }
            richTextBox1.Text = "";
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != "")
            {
                Clipboard.SetDataObject(richTextBox1.Text);
            }
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
            {
                richTextBox1.Text += (String)iData.GetData(DataFormats.Text);
            }
        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Open_Click(object sender, EventArgs e)
        {
            String text = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                using (StreamReader sr = new StreamReader(file))
                {
                    richTextBox1.LoadFile(file, RichTextBoxStreamType.PlainText);
                }
                richTextBox1.Text = text;
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            SaveFileDialog1.Filter = "文本文档(*.txt)|*.txt|rtf文档(*.rtf)|*.rtf";
            if (SaveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                string str = SaveFileDialog1.FileName;
                richTextBox1.LoadFile(str, RichTextBoxStreamType.PlainText);
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            SaveFileDialog1.Filter = "文本文档(*.txt)|*.txt|rtf文档(*.rtf)|*.rtf";
            if (SaveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                string str = SaveFileDialog1.FileName;
                richTextBox1.LoadFile(str, RichTextBoxStreamType.PlainText);
            }
        }

        private void Shear_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != "")
            {
                Clipboard.SetDataObject(richTextBox1.Text);
            }
            richTextBox1.Text = "";
        }

        private void Copy_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != "")
            {
                Clipboard.SetDataObject(richTextBox1.Text);
            }
        }

        private void Paste_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
            {
                richTextBox1.Text += (String)iData.GetData(DataFormats.Text);
            }
        }

        private void Font_Click(object sender, EventArgs e)
        {
            richTextBox1.Font = new Font("宋体", Int32.Parse(Font.Text), FontStyle.Bold);
        }

        private void toolStripComboBox2_Click(object sender, EventArgs e)
        {
            if (toolStripComboBox2.Text == "红色")
            {
                richTextBox1.ForeColor = Color.Red;
            }
            if (toolStripComboBox2.Text == "绿色")
            {
                richTextBox1.ForeColor = Color.Green;
            }
            if (toolStripComboBox2.Text == "蓝色")
            {
                richTextBox1.ForeColor = Color.Blue;
            }
        }

        private void toolStripComboBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (toolStripComboBox2.Text == "红色")
            {
                richTextBox1.ForeColor = Color.Red;
            }
            if (toolStripComboBox2.Text == "绿色")
            {
                richTextBox1.ForeColor = Color.Green;
            }
            if (toolStripComboBox2.Text == "蓝色")
            {
                richTextBox1.ForeColor = Color.Blue;
            }
        }

        private void toolStripComboBox2_Enter(object sender, EventArgs e)
        {
            if (toolStripComboBox2.Text == "红色")
            {
                richTextBox1.ForeColor = Color.Red;
            }
            if (toolStripComboBox2.Text == "绿色")
            {
                richTextBox1.ForeColor = Color.Green;
            }
            if (toolStripComboBox2.Text == "蓝色")
            {
                richTextBox1.ForeColor = Color.Blue;
            }
        }

        private void 黑体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //richTextBox1.Font = new Font("黑体", Int32.Parse(Font.Text), FontStyle.Bold);
        }

        private void 宋体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //richTextBox1.Font = new Font("宋体", Int32.Parse(Font.Text), FontStyle.Bold);
        }

        private void 仿宋ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //richTextBox1.Font = new Font("仿宋", Int32.Parse(Font.Text), FontStyle.Bold);
        }

        private void 红色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.ForeColor = Color.Red;
        }

        private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.ForeColor = Color.Green;
        }

        private void 蓝色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.ForeColor = Color.Blue;
        }
    }
}

3 实现效果

在这里插入图片描述

以上是关于C#简易记事本的实现的主要内容,如果未能解决你的问题,请参考以下文章

Vue 实现简易记事本

Vue 实现简易记事本

C#当中利用Attribute实现简易AOP

是否可以动态编译和执行 C# 代码片段?

作业三:用c#实现一个记事本

C#常用代码片段备忘