天天看點

C#動态添加右鍵菜單、關聯資料及相關事件使用的示例

上次研究了一下正常的菜單,今天來研究一下控件的右鍵菜單項,差不太多。

先添加2個右鍵菜單控件,一個直接添加菜單,另一個用來動态添加菜單

C#動态添加右鍵菜單、關聯資料及相關事件使用的示例

分别給label1和label2關聯菜單:

C#動态添加右鍵菜單、關聯資料及相關事件使用的示例

直接上代碼了:

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //直接給寫好的菜單加事件
            //主要是周遊所有菜單及子菜單
            //根據點選菜單文本做出判斷
            AddMenuEvents(contextMenuStrip1);

            //動态添加菜單,并添加事件
            //可以用遞歸來添加子菜單
            //這裡我隻加了一層
            //加了 tag 屬性傳遞資料
            GetMenus();
        }

        //測試的資料源
        DataTable GetDT()
        {
            DataTable mydt = new DataTable();

            DataColumn dc0 = mydt.Columns.Add("ID", typeof(Int32));
            DataColumn dc1 = new DataColumn();
            mydt.Columns.Add(dc1);

            for (int i = 0; i < 5; i++)
            {
                DataRow newRow = mydt.NewRow();
                newRow[0] = i;

                //這個用來做與菜單關聯的資料
                //實際上想傳什麼就傳什麼
                //MenuCMD 中會用到
                newRow[1] = i + 100;

                //在最後面插入資料
                mydt.Rows.Add(newRow);
            }

            return mydt;
        }

        void GetMenus()
        {
            DataTable dt = GetDT();

            int row = dt.Rows.Count;
            for (int i = 0; i < row; i++)
            {
                contextMenuStrip2.Items.Add(dt.Rows[i][0].ToString().Trim());
                contextMenuStrip2.Items[i].Tag = dt.Rows[i][1].ToString().Trim();
            }

            AddMenuEvents(contextMenuStrip2);
        }

        void AddMenuEvents(ContextMenuStrip m)
        {
            foreach (ToolStripItem items in m.Items)
            {
                if (items is ToolStripMenuItem)
                {
                    ChildMenu((ToolStripMenuItem)items);
                }
            }
        }
        void ChildMenu(ToolStripMenuItem menu)
        {
            if (menu.HasDropDownItems)
            {
                foreach (ToolStripMenuItem m in menu.DropDownItems)
                {
                    ChildMenu(m);
                }
            }
            else
            {
                menu.Click += new EventHandler(MenuCMD);
            }
        }
        void MenuCMD(object sender, EventArgs e)
        {
            ToolStripMenuItem ts = (sender as ToolStripMenuItem);
            string thecmdstr = ts.Text;

            string thetag = "沒有";
            if(ts.Tag != null) { thetag = ts.Tag.ToString(); }

            switch (thecmdstr)
            {
                case "AA":
                    label2.Text = "AA";
                    break;
                case "0":
                    label2.Text = "資料為 " + thetag;
                    break;
                default:
                    break;
            }
        }
    }
}           

運作結果

C#動态添加右鍵菜單、關聯資料及相關事件使用的示例

繼續閱讀