Invoking eventhandlers for event in another class

  • Thread starter Thread starter jwallison
  • Start date Start date
J

jwallison

I'm working in C# on a visual class that builds its own chevron menu a la
rebar controls, for one or more child toolbars.

Creating a ContextMenu and merging the toolbar.DropDownMenu into my
ContextMenu is easy, but is there a reasonable way to invoke toolbar
ButtonClick from a .Click event handler defined for my ContextMenu?

Time is short, and I see way to much study of System.Reflection to be
practicable... any help would be greatly appreciated.


--
Regards,

Jim Allison
(e-mail address removed)
(de-mung by removing '.1')
 
Hi jwalisson,
Are talking about the standard Windows Forms' TooBar class or you have your
own.

B\rgds
100
 
I'd like to keep it "generic" and invoke ToolBar.ButtonClick from
MenuItem.Click.

Right now, I just do a MergeMenu() on an instance of ToolBar class'
DropDownMenu in order to add that "submenu" to my chevron menu - I'd like to
stay "generic" and invoke the same event handler defined for
ToolBar.ButtonClick rather than creating another ToolBar-derived class and
tying the two implementations together.
 
Hi Jim,

Thanks for your post!

As my understanding now, you want to fire the click event on your context
menuitem in the toolbar.buttonclick event. right?

You may invoke the "OnClick" method of the corrsponding menuitem by
reflection like the sample below:
<code>
private void toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
Type t = contextMenu2.MenuItems[0].GetType();
System.Diagnostics.Debug.WriteLine(t.ToString());

t.InvokeMember("OnClick",

BindingFlags.NonPublic|BindingFlags.InvokeMethod|BindingFlags.Instance,null,
contextMenu2.MenuItems[0],new object[]{EventArgs.Empty});
}
</code>

InvokeMember by Reflection needs ReflectionPemission,for reflecting members
that are not public.

However, generally it's not recommend to re-use code in this way. You
should write your code in a seperate method and call it in your menu
handlers and button click handler.

Does this solve your problem?
If you still have problem on this issue, please reply this thread to let me
know.
Thanks!






Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Thanks for your response, Ying-Shen -

Actually, I am wanting to do the opposite - invoke the ToolBar.ButtonClick
event handler(s) via the ContextMenu's MenuItem.Click - creating a
ToolBarButtonEvent and invoking ToolBar.ButtonClick.

The reason why I would rather raise the event/invoke the EventHandler list
"anonymously" (without coding delegates for every menu item) is in order to
avoid creating a specific implementation for every toolbar that is "managed"
by this pseudo-rebar-control.. In that way, I anticipate being able to tie
events to the toolbar(s) without ALSO tying the events to the "rebar"
control.

--
Regards,

Jim Allison
(e-mail address removed)
(de-mung by removing '.1')
 
Hi jwallison,
If you don't want to inherit form ToolBar class you don't have any otptions
but using reflection. How the other guy said I don't thing it is a good way
to reuse a class though.
So my siggestion is still to inherit form ToolBar class. In your new class
you can implement InvokeButtonClick(TooBarButtonClickEventArgs e); this
method will call parent's (generic TollBar class)
OnButtonClick(TooBarButtonClickEventArgs e) which will raise the event.
Since OnButtonClick is protected the reflection and inheritance are your two
only options, I think.

HTH
B\rgds
100
 
Hi Jim,

Thanks for the clarification.
I aggree with our community member, you may call the Button.OnButtonClick
method by reflection,
I'd like to provide a sample snippet for this, in fact, the code didn't
change much.
<code>
private void button2_Click(object sender, System.EventArgs e)
{
Type t = toolBar1.GetType();
System.Diagnostics.Debug.WriteLine(t.ToString());
// click on the first button.
ToolBarButtonClickEventArgs arg = new
ToolBarButtonClickEventArgs(toolBar1.Buttons[0]);

t.InvokeMember("OnButtonClick",
BindingFlags.NonPublic|BindingFlags.InvokeMethod|BindingFlags.Instance,
null, toolBar1,new object[]{arg});
}
</code>

If you still have questions on this issue, please reply this thread to let
me know.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
I had made the required modifcations yesterday, and yes, it does work.

Sorry I didn't post the results -

thanks again -


--
Regards,

Jim Allison
(e-mail address removed)
(de-mung by removing '.1')
 
Back
Top