Throw an Event when a Variable changes (VB .NET)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bergy
    New Member
    • Mar 2007
    • 89

    Throw an Event when a Variable changes (VB .NET)

    I have a user control class, when a user selects the control (or it's children) the background of the control turns blue. When the user exits, it returns to its "unselected " state. I have accomplished this with simple ENTER/LEAVE events.

    I then put multiple controls into a FlowLayoutPanel and everything works just great, I can add as many controls (with a button attached to the form) as I need.

    Now... I'm trying to delete controls and I need some way to flag which control was selected last -- since the user leaves the control and the LEAVE event is fired when the click the delete button -- the bg color changes and there is no way for me to tell which control was selected last so I can't tell the delete function which control it should delete.

    My solution to this is to add another property to the control, a boolean, named isSelected. I set that in the control on ENTER. What I'd like to do is monitor the isSelected boolean value and when it has changed throw an event so I can handle it in my form.

    I'm asking, how can I monitor a variable in a class, and when the value is changed, throw an event.
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    Originally posted by bergy
    I have a user control class, when a user selects the control (or it's children) the background of the control turns blue. When the user exits, it returns to its "unselected " state. I have accomplished this with simple ENTER/LEAVE events.

    I then put multiple controls into a FlowLayoutPanel and everything works just great, I can add as many controls (with a button attached to the form) as I need.

    Now... I'm trying to delete controls and I need some way to flag which control was selected last -- since the user leaves the control and the LEAVE event is fired when the click the delete button -- the bg color changes and there is no way for me to tell which control was selected last so I can't tell the delete function which control it should delete.

    My solution to this is to add another property to the control, a boolean, named isSelected. I set that in the control on ENTER. What I'd like to do is monitor the isSelected boolean value and when it has changed throw an event so I can handle it in my form.

    I'm asking, how can I monitor a variable in a class, and when the value is changed, throw an event.
    [code=cpp]
    public class MyControl
    {
    private bool _IsSelected = false;

    public delegate void PrivateVariable Changed(object sender, EventArgs e)
    public event PrivateVariable Changed SelectedChanged = new PrivateVariable Changed(EmptyHa ndler);

    private static void EmptyHandler(ob ject sender, EventArgs e)
    { }

    public bool IsSelected
    {
    get { return _IsSelected; }
    set
    {
    if(IsSelected != value)
    SelectedChanged .Invoke();

    _IsSelected = value;
    }
    }
    }
    [/code]


    That will make it so whenever you change IsSelected (mind you, the public property, NOT the private variable. Use the property whenever possible) it will invoke the SelectedChanged event. With that event you can tie your functions to it.

    Comment

    Working...