fooOnYou
fooOnYou

Reputation: 698

Update GUI based on boolean

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.

Upvotes: 0

Views: 174

Answers (2)

Rodrigo Guedes
Rodrigo Guedes

Reputation: 1175

Let me give you a simple example:

button1.IsEnabled = false;

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

Upvotes: 0

Kreg
Kreg

Reputation: 647

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!

Upvotes: 1

Related Questions