程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#小程序—帶有歷史的菜單

C#小程序—帶有歷史的菜單

編輯:C#入門知識

    前天寫的一個小程序,主要功能有用文本方式打開一個文件、改變文字的字體和大小、帶有歷史的菜單。程序寫的很亂,功能也不是太完整,主要在歷史菜單上。只要控件有一個RichTextBox以及MenuStrip。
[csharp]
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 CSharp實例練習_帶歷史的菜單 

    public partial class FrmMain : Form 
    { 
        public FrmMain() 
        { 
            InitializeComponent(); 
 
             //開始時歷史菜單不可見 
            文件1ToolStripMenuItem.Visible=false; 
            文件2ToolStripMenuItem.Visible = false; 
            文件3ToolStripMenuItem.Visible = false; 
            文件4ToolStripMenuItem.Visible = false; 
            文件5ToolStripMenuItem.Visible = false; 
            文件6ToolStripMenuItem.Visible = false; 
            文件7ToolStripMenuItem.Visible = false; 
            文件8ToolStripMenuItem.Visible = false; 
            文件9ToolStripMenuItem.Visible = false; 
            文件10ToolStripMenuItem.Visible = false; 
        } 
 
        //定義變量:程序所在路徑,菜單歷史,是否存在配置文件 
        string path =  System.IO.Directory.GetCurrentDirectory ( ); 
        string[] history = new string[10]; 
 
        //菜單中打開文件函數 
        private void 打開ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            string filename=""; 
            OpenFileDialog filedia = new OpenFileDialog(); 
            if (filedia.ShowDialog() == DialogResult.OK) 
            { 
                filename= filedia.FileName; 
                this.rtboxResult.Text += filename; 
                this.rtboxResult.LoadFile(filename, RichTextBoxStreamType.PlainText); 
                string ShortFileName = filename.Substring(filename.LastIndexOf("\\") + 1); 
                this.Text = ShortFileName; 
 
                //歷史文件存為backup.ini 
                string newpath = path + @"\backup.ini"; 
                //打開一個文件文件,並將其地址存為歷史,在菜單中也要儲存 
                CheckMenu(newpath); 
                StreamWriter sr = new StreamWriter(newpath,true,Encoding.Default); 
                sr.WriteLine(filename); 
                sr.Close(); 
                //加載歷史 
                load(); 
 
            } 
        } 
 
        //退出 
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        //設置字體,父窗口與子窗口之間的通訊,試了好久才出來。 
        private void 字體ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmFont font = new FrmFont(this); 
            font.Show(); 
        } 
 
        //復制 
        private void 復制ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.rtboxResult.Copy(); 
        } 
 
        //粘貼 
        private void 粘貼ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.rtboxResult.Paste(); 
        } 
 
        //剪切 
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.rtboxResult.Cut(); 
        } 
 
        //關閉當前文件 
        private void 關閉ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            rtboxResult.Clear(); 
        } 
 
        //幫助按鈕 
        private void 幫助ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmHelp frm2 = new FrmHelp(); 
            frm2.ShowDialog(); 
        } 
 
        private void FrmMain_Load(object sender, EventArgs e) 
        { 
            //如果配置文件存在,讀取配置文件中的內容 
            load(); 
        } 
 
        //點擊歷史菜單打開文件 
        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
                string temp = ((ToolStripMenuItem)sender).Text; 
                this.rtboxResult.LoadFile(temp, RichTextBoxStreamType.PlainText); 
                string ShortFileName = temp.Substring(temp.LastIndexOf("\\") + 1); 
                this.Text = ShortFileName; 
 
                string newpath = path + @"\backup.ini"; 
                CheckMenu(temp); 
                StreamWriter sr = new StreamWriter(newpath, true, Encoding.Default); 
                sr.WriteLine(temp); 
                sr.Close(); 
        } 
 
        //確定歷史菜單是否顯現 
        private void CheckMenu(string path) 
        { 
            if (文件1ToolStripMenuItem.Text == "") 
            { 
                文件1ToolStripMenuItem.Text = path; 
                文件1ToolStripMenuItem.Visible = true; 
            } 
            else if (文件2ToolStripMenuItem.Text == "") 
            { 
                文件2ToolStripMenuItem.Text = path; 
                文件2ToolStripMenuItem.Visible = true; 
            } 
            else if (文件3ToolStripMenuItem.Text == "") 
            { 
                文件3ToolStripMenuItem.Text = path; 
                文件3ToolStripMenuItem.Visible = true; 
            } 
            else if (文件4ToolStripMenuItem.Text == "") 
            { 
                文件4ToolStripMenuItem.Text = path; 
                文件4ToolStripMenuItem.Visible = true; 
            } 
            else if (文件5ToolStripMenuItem.Text == "") 
            { 
                文件5ToolStripMenuItem.Text = path; 
                文件5ToolStripMenuItem.Visible = true; 
            } 
            else if (文件6ToolStripMenuItem.Text == "") 
            { 
                文件6ToolStripMenuItem.Text = path; 
                文件6ToolStripMenuItem.Visible = true; 
            } 
            else if (文件7ToolStripMenuItem.Text == "") 
            { 
                文件7ToolStripMenuItem.Text = path; 
                文件7ToolStripMenuItem.Visible = true; 
            } 
            else if (文件8ToolStripMenuItem.Text == "") 
            { 
                文件8ToolStripMenuItem.Text = path; 
                文件8ToolStripMenuItem.Visible = true; 
            } 
            else if (文件9ToolStripMenuItem.Text == "") 
            { 
                文件9ToolStripMenuItem.Text = path; 
                文件9ToolStripMenuItem.Visible = true; 
            } 
            else if (文件10ToolStripMenuItem.Text == "") 
            { 
                文件10ToolStripMenuItem.Text = path; 
                文件10ToolStripMenuItem.Visible = true; 
            } 
            else 
            { 
                文件10ToolStripMenuItem.Text = path; 
            } 
        } 
 
        //加載歷史菜單函數 
        private void load() 
        { 
            if (File.Exists(path + @"\backup.ini")) 
            { 
                string temp; 
                string newpath = path + @"\backup.ini"; 
                StreamReader sr = new StreamReader(newpath, Encoding.Default, false); 
                int i = 0; 
                while ((temp = sr.ReadLine()) != null) 
                { 
                    history[i] = temp; 
                    #region 
                    switch (i + 1) 
                    { 
                        case 1: 
                            文件1ToolStripMenuItem.Text = temp; 
                            文件1ToolStripMenuItem.Visible = true; 
                            break; 
                        case 2: 
                            文件2ToolStripMenuItem.Text = history[i]; 
                            文件2ToolStripMenuItem.Visible = true; 
                            break; 
                        case 3: 
                            文件3ToolStripMenuItem.Text = history[i]; 
                            文件3ToolStripMenuItem.Visible = true; 
                            break; 
                        case 4: 
                            文件4ToolStripMenuItem.Text = history[i]; 
                            文件4ToolStripMenuItem.Visible = true; 
                            break; 
                        case 5: 
                            文件5ToolStripMenuItem.Text = history[i]; 
                            文件5ToolStripMenuItem.Visible = true; 
                            break; 
                        case 6: 
                            文件6ToolStripMenuItem.Text = history[i]; 
                            文件6ToolStripMenuItem.Visible = true; 
                            break; 
                        case 7: 
                            文件7ToolStripMenuItem.Text = history[i]; 
                            文件7ToolStripMenuItem.Visible = true; 
                            break; 
                        case 8: 
                            文件8ToolStripMenuItem.Text = history[i]; 
                            文件8ToolStripMenuItem.Visible = true; 
                            break; 
                        case 9: 
                            文件9ToolStripMenuItem.Text = history[i]; 
                            文件9ToolStripMenuItem.Visible = true; 
                            break; 
                        case 10: 
                            文件10ToolStripMenuItem.Text = history[i]; 
                            文件10ToolStripMenuItem.Visible = true; 
                            break; 
                    } 
                    #endregion 
                    i++; 
                } 
                sr.Close(); 
            } 
        } 
    } 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved