背景:
由于我之前做了动态菜单加载,但是某些原因让我使用固定的菜单配置,
总结:
然后我就把这固定设置菜单和动态生成菜单及绑定事件都弄了
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 }
设计器默认代码(这里由于菜单设置不好描述,所以把设计器的代码贴出来可以看)
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; }
一级菜单属性
二级子菜单属性截图