2

This question is in reference to another question, mine is similar but I am asking for help beyond what I have read in this answer:

Raise an event whenever a property's value changed?

EDIT: What I am trying to accomplish is a global message "service" within the application such that I can write to the message variable from different places within the application and have the User Interface (winform) pick up on the fact that there was some change to that variable and based upon the event, I can read the message variable and display its content to the user. I hope this makes more sense now.

First, I am new to the world of C# and while I understand the code written as the most accepted answer, where I fail, is to understand the final implementation. If I place this code in a .cs file and I use the namespace in winform file how do I finalize the implementation? In my case, I would want to implement the class in the winform file so I can watch for an event to occur. Once the event occurred I would write some information to the user via the winform interface. I think I would need to use the "get" of the string...but not sure how the implementation would go? I apologize in advance if this doesn't make sense, I am trying to piece this all together. Thanks for any help on this!

For reference, I have start with the answer provide and altered it for my purposes:

public class Messaging : INotifyPropertyChanged
{
    private string dLMessage;

    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public string DLMessage
    {
        get { return dLMessage; }
        set
        {
            if (value != dLMessage)
            {
                dLMessage = value;
                OnPropertyChanged("DLMessage");
                OnDLMessageChanged(EventArgs.Empty);
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    protected void OnDLMessageChanged(EventArgs e)
    {
        EventHandler handler = DLMessageChanged;
        if (handler != null)
            handler(this, e);
    }

    public event EventHandler DLMessageChanged;

}
Community
  • 1
  • 1

1 Answers1

0

Edit


According to your edited question, there are many different ways. One of those is making DLMesaage property and its change event static:

public class Messaging 
{
    private static string dLMessage;

    public static string DLMessage
    {
        get { return dLMessage; }
        set
        {
            if (value != dLMessage)
            {
                dLMessage = value;
                OnDLMessageChanged(EventArgs.Empty);
            }
        }
    }

    protected static void OnDLMessageChanged(EventArgs e)
    {
        EventHandler handler = DLMessageChanged;
        if (handler != null)
            handler(null, e);
    }

    public static event EventHandler DLMessageChanged;

}

and then subscribe for event this way in all your different forms (for example in form load event)

Messaging.DLMessageChanged += msg_DLMessageChanged;

having this function in that form:

void msg_DLMessageChanged(object sender, EventArgs e)
{
    MessageBox.Show("at last changed!");
}

You can unsubscribe for event this way:

Messaging.DLMessageChanged -= msg_DLMessageChanged;

For example if you subscribed for event in some forms, you can put unsubscribe code in Dispose override:

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        Messaging.DLMessageChanged -= msg_DLMessageChanged;
        components.Dispose();
    }
    base.Dispose(disposing);
}

This way if you close the form, the event never be handled in that form.

Please note that I am keeping things simple in order to get your job done with minimum changes.

Original


Put this some where you instantiate Messageing instance for example in your form's constructor or load event handler:

Messaging msg = new Messaging();
msg.DLMessageChanged += msg_DLMessageChanged;

Add this to the form:

void msg_DLMessageChanged(object sender, EventArgs e)
{
    MessageBox.Show("at last changed!");
    //You can access the new value using Messaging.DLMessage 
}

And also it seems you don't need to implement INotifyPropertyChanged if you only want DLMessageChanged. Now you are raising both events.

Or in case you want to use PropertyChanged event, put this some where you instantiate Messageing instance for example in your form's constructor or load event handler:

Messaging msg = new Messaging();
msg.PropertyChanged+= msg_PropertyChanged;

Add this to the form:

void msg_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "DLMessage")
        MessageBox.Show("at last changed!");
}
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I am trying the implementation on this but still having difficulties. I am going to edit the question and add more details. Thank you for the help so far... – Kevin Zarzycki Sep 14 '15 at 15:44
  • So in order to initialize an event, I would perform a write to the variable dLMessage from somewhere in the code and it should fire off an event and that event would be captured, eventually displaying the popup box, is that correct? – Kevin Zarzycki Sep 14 '15 at 19:53
  • @KevinZarzycki Yes, every form that subscribed for event will receive it and will show a messagebox (if you put a message box in the method that handles event in each form) – Reza Aghaei Sep 14 '15 at 19:58
  • @KevinZarzycki In fact calling `handler(this, e);` will cause execution of all handlers that has been attached to this event. – Reza Aghaei Sep 14 '15 at 20:01
  • @KevinZarzycki you can read the edit to learn how to unsubscribe for event. – Reza Aghaei Sep 14 '15 at 20:17
  • You are amazing! Thank you so much for taking the time to provide such a detailed response. I was able to achieve the desired results. I could not have done it without your help! How do I mark this as the accepted answer? – Kevin Zarzycki Sep 15 '15 at 03:33
  • @KevinZarzycki Happy to hear from you that it was helpful:) In StackOverflow when you find an answer helpful you can kindly accept it as answer by click on the check mark at the left side of the answer, and also you can vote an answer up/down by click on up/down arrow in the left side of answer. You can accept only one answer, but you can vote up as many answer as you found useful, including accepted answer. – Reza Aghaei Sep 15 '15 at 03:54