Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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.
Posted 26-Oct-10 8:49am
Craigjw351

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 26 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100