Thank You in advanced !!!!
Does it have to be the same Event that gets called from the Menu and Toolbar. Can you not have an event handler for each that simply calls the same method:
private void newMenuItem_Click(object sender, System.EventArgs e)
{
PerformAction( );
}
private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
PerformAction( );
}
private void PerformAction( )
{
// Do Common Stuff Here
}
private void tlbMain_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
mainFormMenu.MenuItems[AnIndex].Click(); //why I can't run this line of
code ????
}
Whatever, I greatly would appreciate either if you give me an idea why the
above code doesn't compile or posting your code of implementing this
situation.
Greatly appreciated, thanks a lot.
"fbhcah" <anon...@discussions.microsoft.com> wrote in message
news:E4C8EA23-BB7E-4A4D...@microsoft.com...
> When there a number of duplicates in Menu items and tool bar buttons, this
can be quite a little problem. What I did was to create a centralized
overloaded event handler, which is invoked when either a menu click or
toolbar button click happens.
>
> Depending upon the overload I would pass a string to another method, which
has all switch-cases, which would finally do the job. This would ensure that
expandability is maximum, you jiust need to keep adding new cases.
>
> If you want, I could post the code.
>
> HTH,
> fbhcah
>
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.
HTH,
fbhcah
private void tlbMain_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
mainFormMenu.MenuItems[AnIndex].Click();
}
How can I fire the event of the menuItem mainFormMenu.MenuItems[AnIndex]
???????
Thank You very much.
Software Engineer
QuadraMed Corp.
Reston.VA
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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" <anon...@discussions.microsoft.com> wrote in message
news:8EDF13D5-0218-493E...@microsoft.com...
BTW, just FYI, menu items have a Name property @ design time. This is brought into effect by the designer. There is no Name property @ run time. Isn't that interesting? :)
Regards,
fbhcah
Thank You
"fbhcah" <anon...@discussions.microsoft.com> wrote in message
news:7E133C6B-31A5-4D23...@microsoft.com...
At runtime, the actual MenuItem instance is created.
HTH,
fbhcah