Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I have been writing winforms for a while now and I still haven't worked out a decent way of tracking what controls are active at a given time depending on particular conditions, for example, are all text boxes non null or white space, has the appropriate options been selected, in which case enable the OK button.

I toyed around with writing a enumeration for each control and a class with bool attributes for each respective control and when a text box text changed event occurs for example, I called a single method with the control name and value, this method would then set the bool attribute for the control and check to see what controls are now active, given what controls are valid.

My question is, the system I described turned out to be quite messy, can anyone describe a better way, or perhaps know of a site which explains a good way to do this, I have already done a search of the internet and can find very little.

Anyone able to assist please, thanks.

1 solution

The way I see it you have several options, none of them neat:

1) The way you did it, with events
2) Overload controls and give them IsActive properties etc, and make each control call contained controls (if any) to get the ones you are interested in
3) Write a single method which takes a root control and traverses recursively the child control tree contained within it. If you hit a text box for example, you determine its status.

All the above are messy in different ways, personally I'd go for 3 in winforms.

You might also be worth considering taking a step back from the problem and ask why do you need to solve this problem this way? It is possible that a different architectural pattern is more suited to your problem, such as MVC or M-V-VM where the object model decides that something can't be done unless a value is set, or ViewModel might decide this. M-V-VM works well with WPF but I am less sure of it with Winforms. Of course it all depends what you are trying to do, as ever YMMV ,but the you described is certainly one way I've seen it done in the past. Hope this helps!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900