Binding MenuItems and ToolBarButtons ???

G

genc ymeri

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 !!!!
 
G

Guest

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
fbhca
 
G

genc ymeri

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 said:
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.
 
G

Guest

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

fbhca
 
G

genc ymeri

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.
 
G

genc ymeri

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 said:
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? :)
 
G

Guest

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
fbhca
 
G

genc ymeri

thanks a lot !
fbhcah said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top