Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am using an component. Can I clear component's default events and handler.
Posted

You can remove all handlers, but "remove events" is not just impossible; the whole notion makes no sense at all.

Each object instance has some instance members, and some of those members are event instances (let's set aside static members, they are irrelevant, because you say "clear from an object"). "Removing" them would mean the same as "removing" any other member, such as property "Name". For compiled languages, there is no such thing.

(Specifically on the events, I'll tell you more: you cannot even invoke an event on any event instance anywhere except the code of the declaring class itself, not even its derived class. This is an important fool-proof CLR feature, but it confuses many.)

—SA
 
Share this answer
 
Yes.
Try the code

C#
private void RemoveClickEvent(Button b)
        {
            FieldInfo f1 = typeof(Control).GetField("EventClick", 
                BindingFlags.Static | BindingFlags.NonPublic);
            object obj = f1.GetValue(b);
            PropertyInfo pi = b.GetType().GetProperty("Events",  
                BindingFlags.NonPublic | BindingFlags.Instance);
            EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
            list.RemoveHandler(obj, list[obj]);
        }
 
Share this answer
 
Comments
amagitech 3-Nov-15 8:08am    
It's not worked for me. I used your code like this.


var xx = richTextEditorRibbonBar1.QuickAccessToolBar.Items[0];
RemoveClickEvent((RadButtonElement)xx);


private void RemoveClickEvent(RadButtonElement b)
{
FieldInfo f1 = typeof(Control).GetField("EventClick",
BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(b);
PropertyInfo pi = b.GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}

I solved my problem with different method. But it's not answer to my question. I designed another button same appearence with xx . Then I set xx's visibility collapsed.
Debojyoti Saha 3-Nov-15 8:43am    
what is RadButtonElement?
If you are using any other control toolkit or user control then the code may not applicable.
amagitech 3-Nov-15 8:56am    
it is component

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900