I need to have an event attached to all of my BarButtonItems ItemClick event that will get invoked before any other events that may be placed on that form. I wanted to do this in code at runtime so that I wouldn't have to subclass the BarButtonItem.
With a BarButtonItem, I am getting stuck right at the first part, getting the invocation list of the delegate…
Here is a sample of what I have done before:
Dim ItemClickDelegate As [Delegate] = CType(GetType(DevExpress.XtraBars.BarButtonItem).GetField("ItemClick", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public).GetValue(ctrl), [Delegate])
Dim invocationList As [Delegate]() = ItemClickDelegate.GetInvocationList()
However, it seems that a field for the delegate is not even available. A GetFields() call on the type shows that there are no fields at all…
Upon doing some searching, I found out that the field is not always created and that it will depend on how the code for that event is written…
Is there a way to get the invocation list or is it simply not going to be available with the BarButtonItem?
How do you use reflection to get the invocation list of an event delegate in a BarButtonItem?
Answers
Hi Aaron,
I've redesigned your code a bit : You should call the "itemClick" member (its case sensitive) instead, you should use also others BindingFlags in your code.
For example:
Private Sub Form1_Load(sender As Object, e As EventArgs)
Dim o As Object = GetType(DevExpress.XtraBars.BarItem).GetField("itemClick", BindingFlags.NonPublic Or BindingFlags.Static).GetValue(Nothing)
Dim ehl As EventHandlerList = barButtonItem1.GetType().BaseType.GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(barButtonItem1, Nothing) '
Dim iche As New ItemClickEventHandler(MyItemClickHandler)
ehl.AddHandler(o, iche)
'ehl(o);
End Sub 'Form1_Load
Private Sub MyItemClickHandler(sender As Object, e As ItemClickEventArgs)
MessageBox.Show("Hit")
End Sub 'MyItemClickHandler
Thanks,
Andrew
Hi,
I need something similar. I want to remove all CheckedChanged events attached to a CheckEdit control at runtime. For this, I need the delegates of the event in an CheckEdit object. I tried to get them getting fileds with "GetField" and after with "GetInvocationList". I tried too with an EventHandlerList but I don't find it neither.
Anyway to get them? Thanks in advance!
Hello David,
To avoid mixing several questions in one thread, I've created a separate ticket on your behalf: CheckEdit - How to remove all CheckedChanged event handlers using Reflection.
I will address it shortly.