views:

63

answers:

2

Greetings, I would like to ask what kind of design pattern will be the best in the scenario described below: I have a two combobox: option1 option21 option2 option22 option3 option23 option4 option24

there should be a specific combinations of those two comboboxes - only specific values should be displayed in second combobox depends on the values selected in first combobox. For instance: If i choose option1 - only option21 and option22 should be visible. What kind of design patter should i use? I would like to add that I don't use any DB.

+1  A: 

Why does everything have to be a "design pattern"?

It also depends on what your language offers, e.g. in Python you could just do a dict with internal and display values of the dropdowns:

option_tree = {
    ("option1", "Option 1"): {
        ("option21", "Option 2/1"),
        ("option22", "Option 2/2"),
        },
    ("option2", "Option 2"): {
        ("option21", "Option 2/1"),
        ("option23", "Option 2/3"),
        ("option24", "Option 2/4"),
        ...

If you have a specific UI API in mind, maybe you could mention that...most APIs (web rendering frameworks, &c) have a canonical way of implementing things.

Edit: For .NET, the same method may work after a quick Googling:

Have a Dictionary(String -> Vector).

In Dictionary, put the values for the first combobox as key, and map the corresponding values in the second as value. The values for the first combobox can then be set using:

combobox.AddRange(dictionary.Keys)

the second combobox can be used with

secondcombo.AddRange(dictionary.Item(combo.SelectedItem))

If you clear the second one and re-set it w/ above on every change in the first -- done.

Bernd Haug
I'm using c# .net 3.5
niao
+1  A: 

You could create the two datasources as lists. The first list (used in the first combobox) would contain items that have an associated Specification. When the first item is selected, the Specification can be used to filter the second list.

Mark Seemann