WinForm+C#小案例--->写一个记事本程序

需求如下:

  1. 文件菜单中有两个下拉列表:保存、打开
  2. 格式菜单中有一个下拉列表:自动换行点击自动换行---->下拉列表中选项变成取消自动换行,点击取消自动换行变成自动换行,同时附带换行和不换行对应的效果
  3. 样式菜单中有两个下拉列表:字体、颜色,点击字体修改下方输入框中的字体样式,点击颜色修改下方输入框中颜色样式。

效果预览:

创建 Winform 项目并设计界面
编写代码实现切换逻辑
cs 复制代码
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

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

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Title = "打开文件",
                Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*",
                InitialDirectory = "C:\\Users\\Administrator\\Desktop"
            };
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string directory = openFileDialog.FileName;// 目录
                string fileName = openFileDialog.SafeFileName;// 文件
                try
                {
                    using (StreamReader sr = new StreamReader(directory, Encoding.UTF8))
                    {
                        textBox1.Clear();
                        textBox1.Text = sr.ReadToEnd();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("打开文件失败");
                }
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Title = "保存文件",
                Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*",
                InitialDirectory = "E:/桌面",
                DefaultExt = ".txt",
                OverwritePrompt = true,
            };
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string filePath = saveFileDialog.FileName;
                    File.WriteAllText(filePath, textBox1.Text);
                    textBox1.Clear();// 清空输入框中的信息
                    MessageBox.Show("文件保存成功!");
                }
                catch (Exception)
                {
                    MessageBox.Show("保存文件失败");
                }
            }
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.WordWrap = true;
            MessageBox.Show("开启自动换行");
        }

        private void 取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.WordWrap = false;
            MessageBox.Show("关闭自动换行");
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Font = fontDialog.Font;
                MessageBox.Show("字体样式设置成功!");
            }
            else
            {
                MessageBox.Show("取消设置字体样式");
            }
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.ForeColor = colorDialog.Color;
            }
            else
            {
                MessageBox.Show("取消颜色设置");
            }
        }
    }
}
效果演示
相关推荐
皓月斯语3 小时前
B3865 [GESP202309 二级] 小杨的 X 字矩阵 题解
开发语言·c++·矩阵·题解
Highcharts.js11 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
头发还在的女程序员12 小时前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
fai厅的秃头姐!14 小时前
OpenCV——进阶
人工智能·opencv·计算机视觉
爱写代码的小朋友14 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华14 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
小小晓.14 小时前
C++:语句和作用域
开发语言·c++
wanderist.15 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰16 小时前
PHP上传文件
android·开发语言·php
Yeauty18 小时前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频