1. 程式人生 > 實用技巧 >基於使用者控制元件固定選單MenuStrip/ToolStripMenuItem列表及動態選單列表,是上一篇文章的綜合和升級

基於使用者控制元件固定選單MenuStrip/ToolStripMenuItem列表及動態選單列表,是上一篇文章的綜合和升級

背景:

由於我之前做了動態選單載入,但是某些原因讓我使用固定的選單配置,

總結:

然後我就把這固定設定選單和動態生成選單及繫結事件都弄了

cs業務程式碼

 public partial class UcMenu : UserControl
    {
        public UcMenu()
        {
            InitializeComponent();
            //動態繫結選單事件
            //InitView(menuStrip_ItemClicked);
            //繫結選單事件
            
//BindMenuEventHandler(this.menu, menuStrip_ItemClicked); } #region 遍歷繫結選單事件 /// <summary>繫結子選單事件 /// xun-yu.she|2020-08-13 /// </summary> /// <param name="menuItem"></param> /// <param name="eventhandler"></param> private
void BindSubMenuEventHandler(ToolStripMenuItem menuItem, EventHandler eventhandler) { if (menuItem.DropDownItems.Count > 0) { for (int i = 0; i < menuItem.DropDownItems.Count; i++) { if (menuItem.DropDownItems[i] is
ToolStripSeparator) { continue; } else { BindSubMenuEventHandler((ToolStripMenuItem)menuItem.DropDownItems[i], eventhandler); } } } else { menuItem.Click += new EventHandler(eventhandler); } } /// <summary>繫結選單事件 /// xun-yu.she|2020-08-13 /// </summary> /// <param name="Menu">選單</param> /// <param name="eventhandler"></param> public void BindMenuEventHandler(EventHandler eventhandler) { BindMenuEventHandler(this.menu, eventhandler); } /// <summary>繫結選單點選事件 /// xun-yu.she|2020-08-13 /// </summary> /// <param name="menu">選單</param> /// <param name="eventhandler"></param> public void BindMenuEventHandler(MenuStrip menu, EventHandler eventhandler) { if (menu != null) { foreach (ToolStripMenuItem n in menu.Items) { BindSubMenuEventHandler(n, eventhandler); } } else { MessageBox.Show("選單物件為空"); } } #endregion #region 暫不用動態繫結選單 xun-yu.she|2020-08-13 /// <summary>全選單通用事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuStrip_ItemClicked(object sender, EventArgs e) { var tsmi = sender as ToolStripMenuItem; String methodName = tsmi.Name + "_Click"; MethodInfo method = this.GetType().GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (method == null) { MessageBox.Show("找不到方法" + methodName, "發生錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { method.Invoke(this, null); } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsmi.Text + "發生錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// <summary>(暫不用)初始化動態載入選單及事件 /// xun-yu.she|2020-08-13 /// </summary> /// <param name="eventhandler"></param> public void InitView(EventHandler eventhandler) { if (this.menu.Items.Count > 0) { this.menu.Items.Clear(); } MenuStrip ms = this.menu; //新增選單一 var fileItem = ms.Items.AddContextMenu("檔案"); fileItem.DropDownItems.AddContextMenu("tsddmiCreateMock", "新建模擬資料", eventhandler); fileItem.DropDownItems.AddContextMenu("-"); fileItem.DropDownItems.AddContextMenu("tsddmiOpenFile", "開啟檔案", eventhandler); fileItem.DropDownItems.AddContextMenu("-"); fileItem.DropDownItems.AddContextMenu("tsddmiOpenFile", "開啟檔案", eventhandler); fileItem.DropDownItems.AddContextMenu("-"); fileItem.DropDownItems.AddContextMenu("tsddmiSave", "儲存", eventhandler); fileItem.DropDownItems.AddContextMenu("-"); fileItem.DropDownItems.AddContextMenu("tsddmiSaveAs", "另存為", eventhandler); //新增選單二 var editItem = ms.Items.AddContextMenu("編輯"); editItem.DropDownItems.AddContextMenu("tsmiClearData", "清空資料", eventhandler); editItem.DropDownItems.AddContextMenu("-"); editItem.DropDownItems.AddContextMenu("tsmiMergeOtherFile", "合併入其他檔案", eventhandler); editItem.DropDownItems.AddContextMenu("-"); editItem.DropDownItems.AddContextMenu("tsmiMergeLonggongQAData", "合併入龍宮QA資料", eventhandler); editItem.DropDownItems.AddContextMenu("-"); editItem.DropDownItems.AddContextMenu("tsmiMergeLonggongProData", "合併入龍宮正式資料", eventhandler); //新增選單三 ms.Items.AddContextMenu("tsmiCompare", "比對", eventhandler); //新增選單四 ms.Items.AddContextMenu("tsmiReportGeneration", "生成報表", eventhandler); } #endregion }
View Code

設計器預設程式碼(這裡由於選單設定不好描述,所以把設計器的程式碼貼出來可以看)

    partial class UcMenu
    {
        /// <summary> 
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 元件設計器生成的程式碼

        /// <summary> 
        /// 設計器支援所需的方法 - 不要
        /// 使用程式碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.menu = new System.Windows.Forms.MenuStrip();
            this.tsmiFile = new System.Windows.Forms.ToolStripMenuItem();
            this.tsddmiCreateMock = new System.Windows.Forms.ToolStripMenuItem();
            this.tsddmiOpenFile = new System.Windows.Forms.ToolStripMenuItem();
            this.tsddmiSave = new System.Windows.Forms.ToolStripMenuItem();
            this.tsddmiSaveAs = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiEdit = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiClearData = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiMergeOtherFile = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiMergeLonggongQAData = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiMergeLonggongProData = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiCompare = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiReportGeneration = new System.Windows.Forms.ToolStripMenuItem();
            this.menu.SuspendLayout();
            this.SuspendLayout();
            // 
            // menu
            // 
            this.menu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsmiFile,
            this.tsmiEdit,
            this.tsmiCompare,
            this.tsmiReportGeneration});
            this.menu.Location = new System.Drawing.Point(0, 0);
            this.menu.Name = "menu";
            this.menu.Size = new System.Drawing.Size(388, 25);
            this.menu.TabIndex = 1;
            this.menu.Text = "檔案";
            // 
            // tsmiFile
            // 
            this.tsmiFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsddmiCreateMock,
            this.tsddmiOpenFile,
            this.tsddmiSave,
            this.tsddmiSaveAs});
            this.tsmiFile.Name = "tsmiFile";
            this.tsmiFile.Size = new System.Drawing.Size(44, 21);
            this.tsmiFile.Text = "檔案";
            // 
            // tsddmiCreateMock
            // 
            this.tsddmiCreateMock.Name = "tsddmiCreateMock";
            this.tsddmiCreateMock.Size = new System.Drawing.Size(148, 22);
            this.tsddmiCreateMock.Text = "新建模擬資料";
            // 
            // tsddmiOpenFile
            // 
            this.tsddmiOpenFile.Name = "tsddmiOpenFile";
            this.tsddmiOpenFile.Size = new System.Drawing.Size(148, 22);
            this.tsddmiOpenFile.Text = "開啟檔案";
            // 
            // tsddmiSave
            // 
            this.tsddmiSave.Name = "tsddmiSave";
            this.tsddmiSave.Size = new System.Drawing.Size(148, 22);
            this.tsddmiSave.Text = "儲存";
            // 
            // tsddmiSaveAs
            // 
            this.tsddmiSaveAs.Name = "tsddmiSaveAs";
            this.tsddmiSaveAs.Size = new System.Drawing.Size(148, 22);
            this.tsddmiSaveAs.Text = "另存為";
            // 
            // tsmiEdit
            // 
            this.tsmiEdit.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsmiClearData,
            this.tsmiMergeOtherFile,
            this.tsmiMergeLonggongQAData,
            this.tsmiMergeLonggongProData});
            this.tsmiEdit.Name = "tsmiEdit";
            this.tsmiEdit.Size = new System.Drawing.Size(44, 21);
            this.tsmiEdit.Text = "編輯";
            // 
            // tsmiClearData
            // 
            this.tsmiClearData.Name = "tsmiClearData";
            this.tsmiClearData.Size = new System.Drawing.Size(184, 22);
            this.tsmiClearData.Text = "清空資料";
            // 
            // tsmiMergeOtherFile
            // 
            this.tsmiMergeOtherFile.Name = "tsmiMergeOtherFile";
            this.tsmiMergeOtherFile.Size = new System.Drawing.Size(184, 22);
            this.tsmiMergeOtherFile.Text = "合併入其他檔案";
            // 
            // tsmiMergeLonggongQAData
            // 
            this.tsmiMergeLonggongQAData.Name = "tsmiMergeLonggongQAData";
            this.tsmiMergeLonggongQAData.Size = new System.Drawing.Size(184, 22);
            this.tsmiMergeLonggongQAData.Text = "合併入龍宮QA資料";
            // 
            // tsmiMergeLonggongProData
            // 
            this.tsmiMergeLonggongProData.Name = "tsmiMergeLonggongProData";
            this.tsmiMergeLonggongProData.Size = new System.Drawing.Size(184, 22);
            this.tsmiMergeLonggongProData.Text = "合併入龍宮正式資料";
            // 
            // tsmiCompare
            // 
            this.tsmiCompare.Name = "tsmiCompare";
            this.tsmiCompare.Size = new System.Drawing.Size(44, 21);
            this.tsmiCompare.Text = "比對";
            // 
            // tsmiReportGeneration
            // 
            this.tsmiReportGeneration.Name = "tsmiReportGeneration";
            this.tsmiReportGeneration.Size = new System.Drawing.Size(68, 21);
            this.tsmiReportGeneration.Text = "生成報表";
            // 
            // UcMenu
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.menu);
            this.Name = "UcMenu";
            this.Size = new System.Drawing.Size(388, 24);
            this.menu.ResumeLayout(false);
            this.menu.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menu;
        private System.Windows.Forms.ToolStripMenuItem tsmiFile;
        private System.Windows.Forms.ToolStripMenuItem tsddmiCreateMock;
        private System.Windows.Forms.ToolStripMenuItem tsddmiOpenFile;
        private System.Windows.Forms.ToolStripMenuItem tsddmiSave;
        private System.Windows.Forms.ToolStripMenuItem tsddmiSaveAs;
        private System.Windows.Forms.ToolStripMenuItem tsmiEdit;
        private System.Windows.Forms.ToolStripMenuItem tsmiClearData;
        private System.Windows.Forms.ToolStripMenuItem tsmiMergeOtherFile;
        private System.Windows.Forms.ToolStripMenuItem tsmiMergeLonggongQAData;
        private System.Windows.Forms.ToolStripMenuItem tsmiMergeLonggongProData;
        private System.Windows.Forms.ToolStripMenuItem tsmiCompare;
        private System.Windows.Forms.ToolStripMenuItem tsmiReportGeneration;
    }
View Code

一級選單屬性

二級子選單屬性截圖