简单记事本程序菜单设计

这篇博客介绍了如何使用C#创建一个简单的记事本程序,包括菜单设计和基本功能的实现,如新建、打开、保存、撤销、重复、剪切、复制、粘贴和全选等操作。程序由两个主要部分组成:Form1作为主窗口,NoteForm用于显示和编辑文本。文件操作涉及OpenFileDialog和SaveFileDialog,文本编辑功能利用richtextbox组件实现。
摘要由CSDN通过智能技术生成

Form1.cs的里面的代码!

 

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

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

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NoteForm note = new NoteForm();
            note.MdiParent = this;
            note.Show();
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("简单的记事本程序");
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileUrl = "";
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c://";
            openFileDialog1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";
            openFileDialog1.RestoreDirectory = true;
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                fileUrl = openFileDialog1.FileName;
            }
            NoteForm note = new NoteForm();
            note.MdiParent = this;
            note.Show();
            note.OpenFile(fileUrl);
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.ActiveMdiChild != null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.SaveFile();
            }
        }

        private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.ActiveMdiChild!=null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.FrmUndo();
            }
        }

        private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.FrmRedo();
            }
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.FrmCut();
            }
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.FrmCopy();
            }
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.FrmPaste();
            }
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                NoteForm activeForm = (NoteForm)this.ActiveMdiChild;
                activeForm.FrmSelectAll();
            }
        }
    }
}

 

再添加一个窗口!

相当于客户端!

命名为NotePadApp.cs

代码为

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

namespace NotePadApp
{
    public partial class NoteForm : Form
    {
        string fileName = "";
        public NoteForm()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        public void OpenFile(string fileUrl)
        {
            fileName = fileUrl;
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = File.OpenText(fileUrl))
            {
                string input = "";
                while ((input = sr.ReadLine()) != null)
                {
                    sb.Append(input);
                }
                sr.Close();
            }
            this.richTextBox1.Text = sb.ToString();
        }
        public void SaveFile()
        {
            if(fileName =="")
            {
                if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
                {
                    fileName = saveFileDialog1.FileName;
                }
            }
            using (StreamWriter sw= new StreamWriter(fileName))
            {
                sw.Write(this.richTextBox1.Text);
            }
        }
        public void FrmCopy()
        {
            this.richTextBox1.Copy();
        }
        public void FrmPaste()
        {
            this.richTextBox1.Paste();
        }
        public void FrmCut()
        {
            this.richTextBox1.Cut();
        }
        public void FrmSelectAll()
        {
            this.richTextBox1.SelectAll();
        }
        public void FrmUndo()
        {
            this.richTextBox1.Undo();
        }
        public void FrmRedo()
        {
            this.richTextBox1.Redo();
        }
    }
}

 

最后值得一提的是快速建立菜单项  在工具栏里拖MenuStrip到窗口上,然后在MenuStrip的右上角那个键选择

一个标准菜单集合!在通过编辑项把多余的给删除就行了!

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值