winform自定义按钮菜单

   //填写其他报表按钮
        private void btnWriteRep_Click(object sender, EventArgs e)
        {
            try
            {
                Point p1 = new Point();
                Point p = this.btnWriteRep.Location;

                p1.X = p.X;
                p1.Y = p.Y + btnWriteRep.Height;
                contextmenustrip.Show(this.flowLayoutPanel3, p1);              
            }
            catch (Exception ex)
            {

            }
        }
        //加载填写其他报表下拉按钮菜单
        private bool LoadDropButton()
        {
            try
            {
                contextmenustrip = new ContextMenuStrip();//下拉菜单
                contextmenustrip.Font = new Font("宋体", 10f);
                List<ReportListItem> reportlist = basedeclar.GetReportListItem("Y");
                foreach (ReportListItem rep in reportlist)
                {
                    contextmenustrip.Items.Add(AddMenu("[" + rep.Bbzt_mc + "]" + rep.Name, rep.Value, rep.Bbzt_mc));
                }
                //分隔线
                ToolStripSeparator line = new ToolStripSeparator();
                contextmenustrip.Items.Add(line);

                contextmenustrip.Items.Add(AddMenu("选择更多报表......", "", ""));
                contextmenustrip.ItemClicked += new ToolStripItemClickedEventHandler(contextmenustrip_ItemClicked);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("加载按钮(填写其他报表)菜单失败");
                return false;
            }
        }
        //下拉项单击事件
        void contextmenustrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                if (isChanged)
                {
                    if (MessageBox.Show("数据有变化,是否保存", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        btnSave_Click(sender, null);
                    }
                    else
                    {
                        //LoadAxCellData();
                        axCell1.ReadFromBuffer(PreBytes);                       
                    }
                    isChanged = false;
                }
                //选择更多报表显示所有报表
                if (e.ClickedItem.Text.Contains("选择更多报表"))
                {
                    isCloseMenu = true;
                    contextmenustrip.Items.RemoveAt(contextmenustrip.Items.Count - 1);
                    List<ReportListItem> reportlist = basedeclar.GetReportListItem("N");
                    foreach (ReportListItem rep in reportlist)
                    {
                        contextmenustrip.Items.Add(AddMenu("[" + rep.Bbzt_mc + "]" + rep.Name, rep.Value, rep.Bbzt_mc));
                    }
                    contextmenustrip.Closing += new ToolStripDropDownClosingEventHandler(contextmenustrip_Closing);
                }
                else
                {
                    isCloseMenu = false;
                    //axCell1.SetSheetVisible(axCell1.GetCurSheet(), false);
                    //axCell1.SetSheetVisible(axCell1.GetSheetIndex(e.ClickedItem.Name), true);
                    axCell1.SetCurSheet(axCell1.GetSheetIndex(e.ClickedItem.Name));                   
                }
            }
            catch (Exception ex)
            {

            }
        }
        //下拉菜单关闭判断是否关闭
        void contextmenustrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (isCloseMenu)
            {
                if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked || e.CloseReason == ToolStripDropDownCloseReason.Keyboard)
                    e.Cancel = false;
                else
                    e.Cancel = true;
            }
        }

原文地址:https://www.cnblogs.com/dachuang/p/4785701.html