Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events 1

Status
Not open for further replies.

tzzdvd

Programmer
Aug 17, 2001
52
IT
Hi,
I have described a class with a lot of members with a lot of situation that can change the value of a particular variable member. I would like to intercept every time this variable changes its value in order to perform other things due to the new value.
Is it possible to handle the changing of the variable with an event? If I have understood well, the event _TextChanged of the Text box does the same as I wish on the string variable "Text" member of that class.
If so, how can I write my code?

Thank's

Davide
 
Here is a possible solution that uses a property for triggering the event.

- Create a custom EventArgs (VariableChangedEventArgs) that gives you the possibility to retrieve the new/old value

- Create a delegate (VariableChangedEventHandler) to implement the event

- Create an event (VariableChanged) that needs to be triggered when the variable changes

- Implement your string as a private property. Add code to the SET part of the property to trigger the event.

- Link the event to a method in your class

Code:
public partial class Form1 : Form
{
    [COLOR=green]// Event handler that contains the old and new value of the monitored variable[/color green]
    public class [COLOR=blue][b]VariableChangedEventArgs[/b][/color blue] : EventArgs
    {
        private string _previousValue;
        private string _newValue;

        public string PreviousValue
        {
            get { return _previousValue; }
            set { _previousValue = value; }
        }

        public string NewValue
        {
            get { return _newValue; }
            set { _newValue = value; }
        }

        public VariableChangedEventArgs(string previousValue, string newValue)
        {
            this.PreviousValue = previousValue;
            this.NewValue = newValue;
        }
    }

[COLOR=green]    // Event handler to be used for implementing the event[/color green]
    public delegate void [b][COLOR=blue]VariableChangedEventHandler[/color blue][/b](object sender, VariableChangedEventArgs e);

    [COLOR=green]// Event that will be triggered when a change has occured[/color green]
    public event [b][COLOR=blue]VariableChangedEventHandler[/color blue][/b] VariableChanged;


[COLOR=green]    // Local variable that is monitored (PRIVATE)[/color green]
    private string _monitoredVariable;

[COLOR=green]    // Property that triggers the event each time the value has changed[/color green]
    public string MonitoredVariable
    {
        get { return _monitoredVariable; }
        set 
        {[b][COLOR=red]
            string previousValue = this.MonitoredVariable;                

            _monitoredVariable = value;

[COLOR=green]            // Only trigger the event when it is implemented![/color green]
            if (VariableChanged != null)
            {
                VariableChangedEventArgs e = new VariableChangedEventArgs(previousValue, value);
                this.VariableChanged(this, e);
            }[/color red][/b]
        }
    }

    public Form1()
    {
        InitializeComponent();[b][COLOR=red]
        this.VariableChanged += new VariableChangedEventHandler(LocalVariableChanged);[/color red][/b]
    }

    public void LocalVariableChanged (object sender, VariableChangedEventArgs e)
    {
        MessageBox.Show(String.Format("Variable changed! (old value: {0}; new value: {1}", e.PreviousValue, e.NewValue));
    }

    private void btnChangeValue_Click(object sender, EventArgs e)
    {
        this.MonitoredVariable = "New value";
    }
}

Greetz,

Geert Verhoeven
 
Nice,
it sounds good.
I'll try it as soon as possible.
Thank's

Bye
Davide
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top