43【WinForm案例】WinForm做一个简单记事本

简介: WinForm使用C#语言做一个简单的记事本,满足加载、保存文件,编辑文本、修改文本字体和颜色功能。

前言

WinForm使用C#语言做一个简单的记事本,满足加载、保存文件,编辑文本、修改文本字体和颜色功能。


一、运行使用效果

在这里插入图片描述

二、界面设计

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • menuscript控件
  • ToolScriptMenuItem
  • RichTextBox

msMain、tsmiFile、tsmiOpen、tsmiOpenFile、tsmiOpenDirectory、tsmiSave、tsmiEdit、tsmiBackColor、tsmiFont、tsmiAbout、rtbEditor

三、代码

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;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt|富文本文件(*.rtf)|*.rtf";
            
            saveFileDialog1.Title = "另存为";
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|富文本文件(*.rtf)|*.rtf";
        }

        //打开文件
        private void tsmiOpenFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fileName = openFileDialog1.FileName;
                    if (fileName.EndsWith(".txt", true, null))
                    {
                        rtbEditor.LoadFile(fileName, RichTextBoxStreamType.PlainText);
                    }
                    else
                    {
                        rtbEditor.LoadFile(fileName, RichTextBoxStreamType.RichText);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        //打开文件夹
        private void tsmiOpenDirectory_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
                    saveFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        //保存文件
        private void tsmiSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (saveFileDialog1.FileName.EndsWith("", true, null))
                    {
                        rtbEditor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    }
                    else
                    {
                        rtbEditor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                } 
            }
        }

        //设置背景颜色
        private void tsmiBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = rtbEditor.SelectionBackColor;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    rtbEditor.SelectionBackColor = colorDialog1.Color;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        //更改字体
        private void tsmiFont_Click(object sender, EventArgs e)
        {
            fontDialog1.Font = rtbEditor.SelectionFont;
            fontDialog1.Color = rtbEditor.SelectionColor;
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                rtbEditor.SelectionFont = fontDialog1.Font;
                rtbEditor.SelectionColor = fontDialog1.Color;
            }
        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            AboutForm about = new AboutForm();
            about.ShowDialog();
        }

        private void tsmiEdit_DropDownOpening(object sender, EventArgs e)
        {
            if (rtbEditor.SelectionLength == 0)
            {
                tsmiBackColor.Enabled = false;
                tsmiFont.Enabled = false;
            }
            else
            {
                tsmiBackColor.Enabled = true;
                tsmiFont.Enabled = true;
            }
        }
    }
}

总结

WinForm做的一个简单版本记事本。

目录
相关文章
|
C# Windows
wpf怎么使用WindowsFormsHost(即winform控件)
原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法:   1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.
5551 0
|
6月前
|
开发者 Windows
Winform
【8月更文挑战第1天】
114 1
|
Java C# 索引
C#之 十九 使用WinForm控件
C#之 十九 使用WinForm控件
250 0
|
Shell C++ C语言
38【WinForm】WinForm常见窗体技术汇总
- 窗体调用外部程序与渐变窗体 - 按回车键跳转窗体中的光标焦点 - 剪切板操作
95 0
|
数据挖掘 Python
【WinForm】WinForm实现音乐播放器
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
117 0
|
SQL 小程序 关系型数据库
使用C#写winform小程序入门
使用C#写winform小程序入门
205 0
|
编译器 C# Windows
C# 编写 WinForm 窗体应用程序(第一期)
WinForm 是 Windows Form 的简称,是基于 .NET Framework 平台的客户端(PC软件)开发技术,一般使用 C# 编程。
C# 编写 WinForm 窗体应用程序(第一期)
|
C# 数据安全/隐私保护 Windows
C#编写WinForm 窗体应用程序(第二期)
消息框在 Windows 操作系统经常用到,例如在将某个文件或文件夹移动到回收站中时系统会自动弹出如下图所示的消息框。
C#编写WinForm 窗体应用程序(第二期)
|
C# 数据安全/隐私保护
C# 编写 WinForm 窗体应用程序(第三期)
文本框 (TextBox) 是在窗体中输入信息时最常用的控件,通过设置文本框属性可以实现多行文本框、密码框等。
C# 编写 WinForm 窗体应用程序(第三期)