yeap, .......I wanted to use the tag property as below but too bad and too
weird I can't call the event method without knowing its name...(honestly I
can't believe it I'm not able to do that with C#).
private void tlbMain_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
mainFormMenu.MenuItems[e.button.tag].Click(this, (System.EventArgs) e );
}
Thanks a lot for your help and your implementation code.
fbhcah said:
mainFormMenu.MenuItems[AnIndex].Click();
The Click is an event...you won't be able to call events like that.
As regards the way I used, heres a decription with some code snippets:
There were two possible event sources in my form: the main menu and
toolbar buttons. Since Toolbarbuttons have a Tag property, I used those to
store unique strings which represent 'commands' - just to determine which
command to execute (used by switch case). Eg: 'Print', 'Save' etc. Since
menu items do not have a Tag property, I mapped all the MenuItem Texts and
put their corresponding command into a hashtable.
Both menu item clicks and toolbarbutton clicks invoke either of the two
overloads of the same method, which differs in arguments like this:
private void HandleUserEvents(object sender, System.EventArgs e)
private void HandleUserEvents(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
The latter would simply cast the event arg parameter to System.EventArgs and call the former.
The former would determine the command to be used depending upon the type of the source like this:
private void HandleUserEvents(object sender, System.EventArgs e)
{
string strSwitchParam = string.Empty;
switch( sender.GetType().Name )
{
case "MenuItem":
if(
hstMenuTextToItemMap.ContainsKey(((MenuItem)sender).Text) )
strSwitchParam = hstMenuTextToItemMap[((MenuItem)sender).Text].ToString();
break;
case "ToolBar":
strSwitchParam = ((ToolBarButtonClickEventArgs)e).Button.Tag.ToString();
break;
}
if( strSwitchParam!=string.Empty )
DoAction(strSwitchParam);
}
The DoAction method is what finally does the whole work. It would accept
the command argument, and depending upon the switch-case, execute them.