Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Binding MenuItems and ToolBarButtons ???

0 views
Skip to first unread message

genc ymeri

unread,
Mar 8, 2004, 10:56:57 AM3/8/04
to
Is there any way of binding the MenuItem events with their correspondive
toolbarbutton ones ? e.g if we have a "New" menuItems how can we fire the
same event when the "New" toolBarButton is clicked ?


Thank You in advanced !!!!


PocketNerd

unread,
Mar 8, 2004, 12:16:12 PM3/8/04
to
Hi Genc,

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
}

genc ymeri

unread,
Mar 8, 2004, 12:45:19 PM3/8/04
to
This is probably what I wanted to do ! e.g , instead of writing the code
for each switch..case I wanted to centralize them in one place and calling
them from an index. I ran in trouble when I wanted to fire the event of
menuItem with index = AnIndex. (please see below)


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
>


fbhcah

unread,
Mar 8, 2004, 1:26:10 PM3/8/04
to
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.

HTH,

fbhcah

genc ymeri

unread,
Mar 8, 2004, 1:32:57 PM3/8/04
to
Well, your way works just well.... but why I can't do this:

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!

genc ymeri

unread,
Mar 8, 2004, 1:42:46 PM3/8/04
to
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" <anon...@discussions.microsoft.com> wrote in message

news:8EDF13D5-0218-493E...@microsoft.com...

fbhcah

unread,
Mar 8, 2004, 2:01:12 PM3/8/04
to
Anytime pal! :) :)

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

genc ymeri

unread,
Mar 8, 2004, 2:57:55 PM3/8/04
to
Really ? Where these menu items are getting instatiating from then? How can
we tell there is no name property @ run time ? (trying to get a menu through
its name in a menucollection ????)

Thank You

"fbhcah" <anon...@discussions.microsoft.com> wrote in message

news:7E133C6B-31A5-4D23...@microsoft.com...

fbhcah

unread,
Mar 8, 2004, 3:31:11 PM3/8/04
to
The MenuItem class does not have a Name property. At design time, the designer extends the MenuItem class and add a Name property - that's the Name property we see @ design time, whose value is set to the MenuItem variable name.

At runtime, the actual MenuItem instance is created.

HTH,
fbhcah

genc ymeri

unread,
Mar 8, 2004, 4:31:35 PM3/8/04
to
thanks a lot !

"fbhcah" <anon...@discussions.microsoft.com> wrote in message
news:E5722FB3-57C4-4F2E...@microsoft.com...
0 new messages