Navigation
Links

Powered by Squarespace
« Heroes | Main | Vista RC1 shutdown notification »
Tuesday
Nov212006

Removing a control’s behavior

In Programming .NET Components, Juval Löwy explains the reasoning behind and how to use the EventHandlerList class (It’s used internally in the Control & derived classes to store event subscriptions).

I found that this architecture easily supports the removal of all behavior*:
public static void ClearEventHandlerList (Component component) {
if (component == null)
throw new ArgumentNullException ("component");
FieldInfo fieldInfo = (typeof (Component)).GetField ("events",
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly |
BindingFlags.ExactBinding);
EventHandlerList currentList =
fieldInfo.GetValue (component) as EventHandlerList;
if (currentList != null)
currentList.Dispose ();
fieldInfo.SetValue (component, null);
}

For example, a security component could easily remove a control’s behavior* when the user does not have permission. Keep in mind that this is not a substitute for declarative or imperative security checks - other methods can continued to refer to the control's state or subscribe to its events in the future.

Other benefits include the ability for a security component to use a ToolTip to notify the user of access denied (ToolTips do not function on disabled controls).

button.Click += new System.EventHandler (this.button_Click);
// the button_Click method is called when the button is clicked
...
ClearEventHandlerList (button);
// button does not have a click event subscriber anymore
// (button_Click is no longer called when the button is clicked)


* behavior as defined by event subscriptions where the event accessor implementation uses the EventHandlerList class.

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Greeting. At my lemonade stand I used to give the first glass away free and charge five dollars for the second glass. The refill contained the antidote. Help me! Please help find sites for: Bariatric surgery information videos. I found only this - stony brook university bariatric surgery . Bariatric surgery, ...

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>