簡單記事本程式菜單設計

來源:互聯網
上載者:User

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的右上方那個鍵選擇

一個標準菜單集合!在通過編輯項把多餘的給刪除就行了!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.