Fire Menu Strip项目在C#Windows窗体中动态单击事件

问题描述 投票:1回答:1

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

例如:

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);
}

[我想从另一个事件中触发点击事件。我只想将菜单条名称作为参数传递给该方法并触发它的相应事件。

Ex:

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

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

不使用条件语句

c# windows event-handling mouseclick-event menustrip
1个回答
0
投票

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

private void ShowMe(string name)
{
    var item = (MenuStripItem)this.Controls[name];
    item.PerformClick();
}
© www.soinside.com 2019 - 2024. All rights reserved.