Click here to Skip to main content
15,300,338 members
Articles / Programming Languages / C#
Posted 1 Nov 2005


9 bookmarked

Static Events

Rate me:
Please Sign up or sign in to vote.
2.48/5 (10 votes)
1 Nov 20053 min read
Refresh all loaded controls by changing the base fields.


I just had a conversation with one of my colleagues and he mentioned the subject of using Static Events which was new to me and I want to investigate it in this article.

Background (Why Static?)

Basically the idea is to have something shared among all the loaded instances of a class and ensure that changing the static property will cause all instances to update their content right away without the changer having to iterate through the existing objects and figure out which ones need to be updated. Kind of building the intelligence into the class so that all instances know what to do when the static property has changed.

This reminds me of the example of exchange rate, which is very well known to those implementing in banking systems: all transactions respect the current exchange rate. But I don’t recall using Static Events for that. We saw this property as a separate object and we made sure that there is only one instance of it at a time. And all instances of transactions knew where to find it when needed. There is a fine difference though. The transactions will not need to know about the changes happening on the exchange rate, rather they will use the last changed value at the time that they use it by requesting the current value. This is not enough when, for example, we want to implement an application where the user interface reacts immediately on changes in the UI characteristics like font, as if it has to happen at real-time. It would be very easy if we could have a static property in the Font class called currentFont and a static method to change that value and a static event to all instances to let them know when they need to update their appearance.

Using the code

It is clear that we need a static field that all instances respect. Let’s say we need a static field called Font that all the labels will use to refresh when this base field has been changed.

public class MyLabel : System.Windows.Forms.Label
    // The static field all class instance will respect
    private static Font font = new Font("Verdana",11);

This field requires a static property setter to allow us to make changes to the base field.

public static Font Font
    set {
        font = value;
        OnMyFontChange(new FontChangedEventArgs(font));

As you can see this is where we set the static variable but we also call the notification method to start the delegate.

private static void OnMyFontChange(FontChangedEventArgs e)
    if (MyFontChanged != null)
        MyFontChanged(null, e);

Now almost everything is set. All we need to do is to make sure that every instance subscribes to this event. And that is what we do in the constructor of the class.

public MyLabel()
    // Every instance subscribes to this event
    MyLabel.MyFontChanged += new FontChangedEventHandler(this.ChangeBaseFont);

The delegated method is where we use the changed value to refresh the UI.

private void ChangeBaseFont(object sender, FontChangedEventArgs e)
    base.Font = e.Font ;


Of course we could access the static field without the need to pass it over and introduce a new EventArgs class but it just happens to be implemented so, and it certainly has nothing to do with this subject.

In the demo, I have provided a test application using this label control and it demonstrates how it will update multiple screens by changing the base Font property.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Asghar Panahy
Systems Engineer
Netherlands Netherlands
I am a Microsoft certified application developer and work on Microsoft platform since 1993. I like to learn how Microsoft Systems have been built up and dig myself into different libraries in Win32 and .NET. I have worked with C++, VB6, Java, C#, ASP, ASP.Net and PHP.

Comments and Discussions

GeneralSingleton Pin
Guido_d1-Nov-05 22:14
MemberGuido_d1-Nov-05 22:14 
GeneralRe: Singleton Pin
Pascal Ganaye1-Nov-05 22:28
MemberPascal Ganaye1-Nov-05 22:28 
GeneralRe: Singleton Pin
Guido_d1-Nov-05 23:10
MemberGuido_d1-Nov-05 23:10 
GeneralRe: Singleton Pin
Pascal Ganaye2-Nov-05 1:18
MemberPascal Ganaye2-Nov-05 1:18 
GeneralRe: Singleton Pin
AsgharPanahy2-Nov-05 23:27
MemberAsgharPanahy2-Nov-05 23:27 
Questionstatic events == bad idea? Pin
wickerman.261-Nov-05 9:14
Memberwickerman.261-Nov-05 9:14 
Take a look at this thread for examples of things that can go wrong with static events. In my experience, they're a bad idea unless implemented very carefully. There are so many other ways to do this - why not use a for or foreach loop to do the same thing? It's a lot simpler, and doesn't have the same garbage-collection risks.

My gut instinct tells me that there'd be a performance penalty as well, having every control listening for a 'master' event. (If this isn't the case, I'd be very interested in seeing performace comparisons...)
AnswerRe: static events == bad idea? Pin
AsgharPanahy3-Nov-05 0:32
MemberAsgharPanahy3-Nov-05 0:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.