我在程序中动态生成了几个菜单,怎么为他们的CLICK添加响应函数啊

解决方案 »

  1.   

    动态添加的菜单.Click += new System.EventHandler(菜单名_Click);
    按TAB键会自动添加的!
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace TestMainMenu
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; private MainMenu myMenu;
    private MenuItem mnuFile, mnuExit, mnuRtime;
    private Button btnAdd, btnRemove, btnClose;
    static int i; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    //InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    // //初始化主菜单
    myMenu = new MainMenu();
    mnuFile = new MenuItem("文件");
    mnuRtime = new MenuItem("运行菜单"); //向主菜单加入菜单
    myMenu.MenuItems.Add(mnuFile);
    myMenu.MenuItems.Add(mnuRtime); //初始化Exit菜单项,设置了事件和快捷键
    mnuExit = new MenuItem("退出",new EventHandler(btnClose_Click),Shortcut.CtrlX);

    //把exit菜单中到file菜单中
    mnuFile.MenuItems.Add(mnuExit); //初始化添加按钮
    btnAdd = new Button();
    //btnAdd.Location = new Point(275,20);
    btnAdd.Text= "&ADD";
    btnAdd.Click += new EventHandler(btnAdd_Click);
    btnAdd.Bounds = new Rectangle(new Point(275,20),new Size(100,30)); //初始化删除按钮
    btnRemove = new Button();
    btnRemove.Text= "&Delete";
    btnRemove.Click += new EventHandler(btnRemove_Click);
    btnRemove.Bounds = new Rectangle(new Point(275,60),new Size(100,30));

    //初始化关闭按钮
    btnClose = new Button();
    btnClose.Text= "&Close";
    btnClose.Click += new EventHandler(btnClose_Click);
    btnClose.Bounds = new Rectangle(new Point(275,100),new Size(100,30)); //在窗体中加入按钮
    this.Controls.AddRange(new System.Windows.Forms.Control[]{btnAdd,btnRemove,btnClose}); //设置窗体的属性
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.Text = "动态处理菜单程序窗口!"; //设置本窗体的主菜单
    this.Menu = myMenu;
    this.Size = new Size(400,250);
    this.CancelButton = btnClose;
    this.StartPosition = FormStartPosition.CenterScreen;
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } protected void btnClose_Click(object sender, System.EventArgs e)
    {
    this.Close();
    } //动态加入菜单 
    protected void btnAdd_Click(object sender, System.EventArgs e)
    {
    mnuRtime.MenuItems.Add("运行菜单" + ( i + 1 ),new EventHandler(mnuRtime_Click));
    i++;
    } //动态删除菜单
    protected void btnRemove_Click(object sender, System.EventArgs e)
    {
    mnuRtime.MenuItems.RemoveAt(i-1);
    i--;
    } protected void mnuRtime_Click(object sender, System.EventArgs e)
    {
    String s = sender.ToString();
    MessageBox.Show("你选择的是 " + s.Substring(53),"菜单选择窗口!",
    MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    }
    }
      

  3.   

    可以在页面加载时为他们的属性加上事件
    如:btn.attributes.add("onclick","事件")
    如果是.net事件
    menuitem.事件+=......(方法)写法记不清了
    在页面的初始里自动生成的代码里例子
      

  4.   

    /// <summary>
            /// 动态创建菜单
            /// </summary>
            /// <param name="sMenuStrip"></param>
            
            private void eBuildMenu(MenuStrip sMenuStrip)
            {
                try
                {
                    if (sMenuStrip != null)
                    {
                        string iSQLStr = "Select * From systemmenuinfo order by menuid asc";
                        int iCount = eCommonFunction.fGetTable(iSQLStr).Rows.Count;                    if (iCount > 0)                    
                            for (int i = 0; i < iCount; i++)                        
                                if (eCommonFunction.fGetFieldValue(iSQLStr, i, "parentmenuid") == "0")
                                {
                                    ToolStripMenuItem sMagicMenu = new ToolStripMenuItem();
                                    sMagicMenu.Name = eCommonFunction.fGetFieldValue(iSQLStr, i, "menuname");
                                    sMagicMenu.Text = eCommonFunction.fGetFieldValue(iSQLStr, i, "menutext");
                                    sMenuStrip.Items.Add(sMagicMenu);                                eCircleMenuNodes(sMagicMenu);
                                }                                            
                    }
                }
                finally
                {
                }
            }
            
            /// <summary>
            /// 菜单子节点循环
            /// </summary>
            /// <param name="sMenuItem"></param>
            private void eCircleMenuNodes(ToolStripMenuItem sMenuItem)
            {
                try
                {
                    string sMenuId = eCommonFunction.fGetFieldValue("Select * From systemmenuinfo Where menuname = '" + sMenuItem.Name + "'", 0, "menuid");
                    int HaveChildsCount = eCommonFunction.fGetTable("Select * From systemmenuinfo Where parentmenuid = '" + sMenuId + "'").Rows.Count;
                    if (HaveChildsCount > 0)
                    {
                        for (int j = 0; j < HaveChildsCount; j++)
                        {
                            ToolStripMenuItem sChildMenu = new ToolStripMenuItem();
                            sChildMenu.Name = eCommonFunction.fGetFieldValue("Select * From systemmenuinfo Where parentmenuid = '" + sMenuId + "'", j, "menuname");
                            sChildMenu.Text = eCommonFunction.fGetFieldValue("Select * From systemmenuinfo Where parentmenuid = '" + sMenuId + "'", j, "menutext");
                            sChildMenu.Click += new System.EventHandler(eMenuClick);
                            sChildMenu.Enabled = false;
                            
                            sMenuItem.DropDownItems.Add(sChildMenu);                        eCircleMenuNodes(sChildMenu);
                        }
                    }
                    else
                        return;
                }
                finally
                {
                }
            }