Radiobutton for Combo box


#1

Hi,

I have three combo boxes with same items. If I select one item from the first combo box then that item should be disabled in the other two combo boxes. How could I achieve this?

Any help is appreciated.


#2

You could use a ComboBox::Listener to respond to changes in the combo boxes and then ComboBox::setItemEnabled to enable/disable the corresponding items in the other boxes.


#3

I tried this.

Say there are three items in the combo boxes: ‘a’, ‘b’, ‘c’. If I select ‘a’ in Combo box1 then select ‘b’ in the same combo box, then both a and b are disable in the other two combo boxes. Instead only b should be disable.

Don’t know the work around for this.


#4

I think this is the sort of logic you want, it assumes all combo boxes have the same number of items (although this could be fixed pretty easily).
I’ve also used an enum for the combo boxes so I can easily loop them which is what this requires. Drop this component into your app or a new Introjucer project to see if its what you need.

[code]class MainComponent : public Component,
public ComboBox::Listener
{
public:
MainComponent();

~MainComponent();

void resized();

void comboBoxChanged (ComboBox* comboBoxThatHasChanged);

private:

enum ComboBoxes
{
    box1 = 0,
    box2,
    box3,
    numComboBoxes
};

OwnedArray<ComboBox> comboBoxes;

};[/code]

[code]MainComponent::MainComponent()
{
for (int i = 0; i < numComboBoxes; ++i)
{
ComboBox* newBox = new ComboBox();
newBox->addListener (this);

    newBox->addItem ("item1", 1);
    newBox->addItem ("item2", 2);
    newBox->addItem ("item3", 3);
    newBox->addItem ("item4", 4);

    addAndMakeVisible (newBox);
    comboBoxes.add(newBox);
}

}

MainComponent::~MainComponent()
{
for (int i = 0; i < numComboBoxes; ++i)
comboBoxes[i]->removeListener (this);
}

void MainComponent::resized()
{
for (int i = 0; i < numComboBoxes; ++i)
comboBoxes[i]->setBounds (5, 5 + (i * 25), getWidth() - 10, 20);
}

void MainComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
{
Array disabledIds;
for (int i = 0; i < numComboBoxes; ++i)
disabledIds.add (comboBoxes[i]->getSelectedId());

for (int c = 0; c < numComboBoxes; ++c)
{
    for (int i = 1; i <= comboBoxes[c]->getNumItems(); ++i)
    {
        if (disabledIds.contains (i))
            comboBoxes[c]->setItemEnabled (i, false);
        else
            comboBoxes[c]->setItemEnabled (i, true);
    }
}

}[/code]


#5

Thank you very much. Appreciate your help.