Try to draw out your options as one or more tables of choices and valid
other controls, this helps a lot in discussions with other people of
which controls should enable/disable each other, I usually find that it
also tends to naturally give me the grouping and ordering of the
controls, (if you can simplify your table by splitting out a given set
of options into a separate table then those options probably make a
separate group). Also it makes it easy to see exclusive choices, (radio
buttons/choices), and multiple options, (check box/multi-choice), to
pick the type of control.
Next I usually would have separate create and event handlers for each of
the groups identified. In the create functions I would build a table(s)
of the controls as I created them - this allows me to get all the
selected values/options without searching for the windows - I can then
create a tupple of the selected values that lets me look up which other
controls should be enables/disabled in a dictionary, (with a suitable
default set), and then run through the table(s) setting the
enable/disable values. This data driven way of working is more complex
than needed for simple items but for anything that is likely to grow or
is already complex it has the advantages of much lower complexity
increases each time a control is added, clearer code, etc., and fewer
lines of code. It also tends to allow better self documentation and
less maintenance problems.
Pseudo code:
Code driven (2 controls):
if control1 == valid:
(dis)enable.control2
elif control2 == valid:
(dis)enable.control1
else:
(dis)enable.control1
(dis)enable.control2
Code driven (3 controls):
if control1 == valid:
(dis)enable.control2
if control2 == valid:
(dis)enable.control3
elif control2 == valid and control3 == valid:
(dis)enable.control1
else:
(dis)enable.control1
(dis)enable.control2
As you can see this rapidly gets huge and messy.
Data Driven :
SelectionDict = { # for two controls
# Values : Enables
# (Ctrl1, Ctrl2):(Ctrl1, Ctrl2)
(False,False):(True, True),
(True, False): (True,False)
(False, True): ( etc
OnSelect(self, evt):
key = ()
enableall = ()
for Ctrl in ControlList:
key += (Ctrl.GetValue(), )
enableall += (True,)
enables = SelectionDict.get(key, enableall)
for n in range(0, len(enables)):
ControlList[n].Enable(enables[n])
In the code driven method when you add a control you need to add a new
action in every branch of your if statements and add new if statements,
it is easy to miss one or more. In the data driven method adding a
control increases the size of the table, (all your changes are in one
place), and no new code it is easy to see which options you are adding.
You can use various methods for generating the key into your dictionary
and you can use dictionaries of dictionaries, etc.
Hope this gives you some ideas.
Gadget/Steve
···
On 29/08/2011 7:01 PM, Stefanie L�ck wrote:
You're right, but I gave only a simple example. I have many choices in
3 groups. For some groups multiple choices are allowed, for others not
(inside and outside the group). Some choices depend on the choice of
the other groups, but I have to show that some combinations don't work
together. How to do this? To use Radiobutton and if the User chooses
a "not working" combination to give a message? Something like that? Or
I make even more groups and use a mixture of radio and checkboxes...
Another question, how I can check all the events without repeating
always one and the same conditional? Is there any "clever" way?
Thanks!
Stefanie