C#编写一个较完整的记事本程序

一、开发环境及拓展资源

Visual Studio 2019
至少需安装 .NET桌面开发在这里插入图片描述

二、创建窗体文件

1、创建窗体文件

在这里插入图片描述

2、配置项目名称及框架

在这里插入图片描述

3、设计界面

创建窗体文件,将控件摆放位置如下,参考系统自带的记事本程序
窗体控件分布

4、窗体添加的控件和组件如下

控件及组件在工具箱查找
窗体所需添加的控件和组件

5、窗体属性

窗体属性

6、快捷键设置

在这里插入图片描述

7、程序属性

  1. 项目属性如下图,在创建项目时就已定好了框架,如果在另一台主机上的框架版本比目前项目框架版本低的话,则运行不起来
    文章末尾有整个程序的压缩包链接可下载,如需直接运行则需下载对应的.NET Framework 4.7.2框架
    项目属性
  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";
        }
    }
  1. 打开:
    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!"); }
    }
  1. 保存:
    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();
            }
        }
    }
  1. 另存为:
    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();
        }
    }
  1. 打印:
    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);
            }
        }
    }
  1. 编辑:
    根据输入是否输入内容控制是否启用功能
    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
  • 11
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值