Adding a routine that fires when a property is changed in a custom class

KKW

Member
Joined
Nov 15, 2013
Messages
7
Programming Experience
10+
Hi!

I'm not even sure my title is correct, so please forgive me.

I have created a class that is used to set properties on controls on Form1 from other forms.
The basic process is that I have essentially created a collection of Public Variables that can be set by any form, and used by any form.
example:
The following code resides on Form1.
     public class PlotLegend
     {
         private string legendLocation;
         private string legendHorizontalPosition;
         private string legendVerticalPosition;
         private string legendVisible;

         public string LegendLocation
         {
            get
            {
                return this.legendLocation;
            }
            set
            {
                this.legendLocation = value;
            }
         }
      }

Real basic - straight forward class.
Form2 can read or write the value of/to LegendLocation (for example).
Form1 can then read the value and act accordingly.

What I want to do is add a function/routine that will execute when LegendLocation is changed.
Example:
Lines added are bolded.
     public class PlotLegend
     {
         private string legendLocation;

         public string LegendLocation
         {
            get
            {
                return this.legendLocation;
            }
            set
            {
                this.legendLocation = value;
                changeLocation(value);
            }
         }

         private void changeLocation(string CurrentLocation)
         {
            switch(CurrentLocation)
            {
               case "Bottom":
                 Form1.Legend1.Location = "Left";
                 break;
               case "Left":
                 Form1.Legend1.Location = "Top";
               // and so on....
         }
      }


The changeLocation(value); routine would change the location of the legend location on Form1.

I don't think this is the correct way to accomplish this, - I get an error message saying I don't have access to Form1.
I can change the protection level in the Form1 designer from private to public, but I really don't want to do that.

Can someone assist?

Thanks!
 
Last edited by a moderator:
For future reference, XCODE tags need an option in order to format code. On this site specifically, it is the C# option you need to format C# code. For other languages, or if you want to add extra formatting, then you need to use CODE tags.

As for your question, what you should be doing is declaring an event in that class that Form1 can handle and then update its own controls. The class shouldn't actually know about Form1. All it does is let the universe know that something has happened and it's up to Form1 or anyone else to listen for that event and react accordingly. I suggest that you follow the Blog link in my signature and check out my post on Custom Events. In short though, it would look something like this:
public string LegendLocation
{
    get
    {
        return this.legendLocation;
    }
    set
    {
        if (this.legendLocation != value)
        {
            this.legendLocation = value;
            this.OnLegendLocationChanged();
        }
    }
}

public event EventHandler LegendLocationChanged;

protected virtual void OnLegendLocationChanged()
{
    if (LegendLocationChanged != null)
    {
        LegendLocationChanged(this, EventArgs.Empty);
    }
}
Form1 would then handle that event just like it would any other. In the event handler, it would retrieve the LegendLocation value from the PlotLegend object that raised the event and use it appropriately.

By the way, you should be using an enumeration for that LegendLocation property, not Strings.
 
Back
Top Bottom