Search code examples
c#windowsevent-handlingmouseclick-eventmenustrip

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


I have already added a menu strip controller and added click events for those menu items.

Ex:

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

Here I want fire above click event from another event. I just want to pass the menu strip name as parameter to the method and fire corresponding event of it.

Ex :

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

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

Without using conditional statements


Solution

  • You can find the control by its name and then call its PerformClick() method:

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