Click here to Skip to main content
Click here to Skip to main content
Go to top

Static Events

, 1 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Refresh all loaded controls by changing the base fields.

Introduction

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 ;
    base.Invalidate();
}

Note:

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.

License

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

Share

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 PinmemberGuido_d1-Nov-05 22:14 
GeneralRe: Singleton PinmemberPascal Ganaye1-Nov-05 22:28 
GeneralRe: Singleton PinmemberGuido_d1-Nov-05 23:10 
GeneralRe: Singleton PinmemberPascal Ganaye2-Nov-05 1:18 
GeneralRe: Singleton PinmemberAsgharPanahy2-Nov-05 23:27 
Questionstatic events == bad idea? Pinmemberwickerman.261-Nov-05 9:14 
AnswerRe: static events == bad idea? PinmemberAsgharPanahy3-Nov-05 0:32 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 1 Nov 2005
Article Copyright 2005 by Asghar Panahy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid