By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

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

bergy
P: 89
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.
Jun 18 '07 #1
Share this Question
Share on Google+
1 Reply


TRScheel
Expert 100+
P: 638
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.
Expand|Select|Wrap|Line Numbers
  1. public class MyControl
  2. {
  3.      private bool _IsSelected = false; 
  4.  
  5.      public delegate void PrivateVariableChanged(object sender, EventArgs e)
  6.      public event PrivateVariableChanged SelectedChanged = new PrivateVariableChanged(EmptyHandler);
  7.  
  8.      private static void EmptyHandler(object sender, EventArgs e)
  9.      { }
  10.  
  11.      public bool IsSelected
  12.      {
  13.           get { return _IsSelected; }
  14.           set 
  15.           {
  16.                if(IsSelected != value)
  17.                      SelectedChanged.Invoke();
  18.  
  19.                _IsSelected = value;
  20.           }
  21.      }
  22. }
  23.  

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.
Jun 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.