在 C# Windows 表单中动态触发菜单条项目单击事件

Fire Menu Strip Item Click Event Dynamically in C# Windows Form

我已经添加了菜单条控制器并为这些菜单项添加了点击事件。

例如:

private void mnuaddTestingBuyersForFigures_Click(object sender, EventArgs e)
{
   frmAddTestingBuyersForFigure obj = new frmAddTestingBuyersForFigure();
   objUserManagerBll.SetAccess(obj, User.UserID);
   IsAlreadyLoded(obj);
}

private void mnuOperatorIncentive_Click(object sender, EventArgs e)
{
   frmOperatorIncentive obj = new frmOperatorIncentive();
   objUserManagerBll.SetAccess(obj, User.UserID);
   IsAlreadyLoded(obj);
}

private void mnuSetUpIncentiveMonthProcess_Click(object sender, EventArgs e)
{
   frmSetUpWeeksForIncentiveMonth obj = new frmSetUpWeeksForIncentiveMonth();
   objUserManagerBll.SetAccess(obj, User.UserID);
   IsAlreadyLoded(obj);
}

这里我想触发来自另一个事件的点击事件。我只想将菜单条名称作为参数传递给方法并触发相应的事件。

例如:

ShowMe("mnuSetUpIncentiveMonthProcess");
//Out put open the frmSetUpWeeksForIncentiveMonth form

ShowMe("mnuOperatorIncentive");
//Out put open the frmOperatorIncentive form

不使用条件语句

您可以通过名称找到控件,然后调用它的PerformClick()方法:

private void ShowMe(string name)
{
    var item = (MenuStripItem)this.Controls[name];
    item.PerformClick();
}