0

Trying to grey out certain buttons/text fields depending on the state of a boolean in my program. The boolean keeps track of if the connection to a subsystem is still up. Initializes to false until it connects, and then a watch dog keeps it updated from there on.

This may happen many times through the execution of the program, and thus I would like to make some sort of monitor that merely watches the state of the boolean and updates the GUI/button properties as appropriate.

My initial thought was to make some sort of event handler for this, but in my searches I found something called "properties" in C# that may make this even easier. Unfortunately I wasn't able to find a ton of information on this technique (initial thread here: How to trigger event when a variable's value is changed?)

So I have come to you folks with the hope that you may be able to give me an idea of the best way to do this.

Thanks,

EDIT:: Not sure if it matters, but the boolean is declared as an extern. This may make things easier, as I noticed in many cases the observer pattern is used when communicating between classes, which is not a concern in this problem.

Community
  • 1
  • 1
fooOnYou
  • 678
  • 5
  • 12
  • Can the subsystem not just call an event or something when the state changes? Presumably, the subsystem would be running its own threads and you would have to BeginInvoke() the signal to the GUI. – Martin James Aug 06 '12 at 21:19

2 Answers2

1

C# properties just provide specialized syntax for getting/setting variables in an object. Since they are just specialized methods, you can really add whatever other functionality you want. From what you have described, I would probably recommend going with a listener... action listeners use a pattern called the "observer", which exactly fits what you're trying to do in this case. You can Google "observer pattern", and you'll get a lot more info on how to use it, and create your own variants, which you may or may not decide to do :)

Good luck!

Kreg
  • 617
  • 1
  • 6
  • 16
  • That does look like what I am trying to do, I will read up a bit on that and see what it'll take. Thanks for the tip! I am going to wait to see if any one else has other recommendations before accepting an answer. – fooOnYou Aug 06 '12 at 20:48
0

Let me give you a simple example:

button1.IsEnabled = false;

To disable a button or a text field you just have to do that.

Rodrigo Guedes
  • 1,149
  • 2
  • 13
  • 27
  • I am aware of that part, but I don't want to put that code inline where I change the state of the variable, and I would even prefer to not have to call a function when the variable changes state. I would much prefer to set up some sort of system that will monitor the state of the variable, and update the GUI as nessesary. More work up front, but highly expandable, and efficient in the long run. – fooOnYou Aug 06 '12 at 20:48