How to add a function that will be triggered when the same item has been selected in the combobox?

I have been trying to override combobox class to add this feature but nothing worked. Combobox calls comoboboxChanged function when we select an item from its popup menu but it does not do anything when we try to reselect the same ( already selected and ticked ) item.

I overrode the drawcombobox and drawComboboxItem to change the font of the popup menu items. But I could not find a way to get the mouse down event of the combobox popup menu. It is defined in its class but it is internal. I just want to get a notification or a flag that I can see if an already selected item has been selected again. In that case I will reset the preset parameters but I couldn’t find anything so far.

How you deal with this in your plugins ? After changing the parameters, you may not want to save it or you may want to load the default settings of the preset. Simply by choosing the same preset should reload them. But it does not because it is not triggering the comboboxchanged :confused: Could you please help me to fix this issue ?

Thank you

You can do it in some tricky but very simple way :wink: After you execute your code (if you have any) in your onChange callback, you should also run this code:

comboBox.onChange = [this]
{
    auto menu = comboBox.getRootMenu();
    auto id = comboBox.getSelectedId();
            
    juce::PopupMenu::MenuItemIterator iterator (*menu);

    while (iterator.next())
    {
        auto item = &iterator.getItem();

        if (item->itemID == id)
        {
            item->setAction ([this] { /* your action here! */ });
        }
        else
        {
            item->setAction (nullptr);
        }
    }
};
2 Likes

Thank you very much :pray:.
Right now it is midnight here :slight_smile: Tomorrow this is going to be the first thing that i will do.
It has been months that i started using juce. I created my own preset management system. I am still working on it but this reelection issue is just killing me :smiley: hopefully, thanks to you, this will be solved :pray:

As an alternative route, which I have taken numerous times, you can create your own Components that have the desired behavior. At first it may seem like re-inventing the wheel, but, for example, I needed some special behavior from a combo-box, but it turns out I only needed the worlds simplest combo-box, ie. a text box with a pop-up, so there was no need to implement all of the flexibility that juce::ComboBox has.

2 Likes

it works perfectly :pray: I have already had a onChange. After adding your code it worked. I was not able to get that current item id from the root menu. I had never used the iterators class before this and I thought item id is also not being changed in the root. This helped a lot.

For the next plugin, I will definitely try to create mine :smiley: