This problem is very easy to solve— but this is not a real problem. The bigger problem is that you're trying to solve it all in UI.
You're approaching the problem form in a completely wrong way. You should not do it via UI along. Later on, you will need to translate it to data. What you're going to do?
You need to start from a data layer, which is missing. This is much like trying to use MVP architectural pattern without M (model). See
http://en.wikipedia.org/wiki/Model-view-presenter[
^],
http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[
^].
Here is what you need to do.
Let's start with
meta-data.
Declare something like this:
[System.Flags]
enum Frameworks { PHP = 1, Java = 2, Dotnet = 4, }
This presents the meta-data of your data model. It should tell your application, in particular, that you need three check boxes to represent data, and their names should be "PHP", "Java", and "Dotnet". Unfortunately, it won't allow you to enter some human-readable names such as "Microsoft .NET". Not to worry; this is advanced item solvable using .NET Attributes and even resource; you can find a comprehensive solution in my article
Human-readable Enumeration Meta-data[
^]. For now, let's keep it simple.
When the type for this data items is created, the data could get different values, like:
Frameworks frameworksUsed = Frameworks.PHP;
Frameworks frameworksToBeUsed = Frameworks.PHP | Frameworks.Dotnet;
Frameworks anotherCase = Frameworks.PHP | Frameworks.Java;
string currentlyUsed = frameworksUsed.ToString();
Are you getting it? You should not use any UI element to present the string of frameworks. You should use data.
Now, how to use meta-data? Your best chance is to auto-generate the set or check boxes iterating through the
enum
members. Unfortunately, this type does not support enumeration (
foreach
). You can work around this limitations. There are simple tricks to do it; or you can use my comprehensive method of iteration. I explained both simple techniques and a comprehensive
Enumeration
class in my other article:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^].
No matter how, you enumerate the frameworks and create check boxes. You can name each one by a name of enumeration members or use a human-readable method I describer in my article. More important, you need to add a tag equal to corresponding
Frameworks
member to each check box.
When this is done, you can add a handle to the event
CheckBox.CheckedChanged
of each check box. This event handle should manipulate bit set of
Frameworks
(of this type) which can be a member of your form, for example. If the event handlers are the instance method of this form (naturally), they will have this instance through "this" pointer, so they can modify a bit (add or remove) as the check box checked state is changed, checked or unchecked.
Each time the handler fires, you should change output of the currently selected frameworks. If this is a label, you should assign its
Text
property to
frameworksUser.ToString()
.
This is just a skeleton, but implementation is pretty easy. More importantly, it can be done in a way which is easy to support. Should you later need to change some of the
enum
members, the UI will be modified nearly (or completely, depending on you UI skills) automatically.
If some fragments of such code is not clear — your follow-up questions are welcome.
Good luck,
—SA