Search code examples
c++buildertcombobox

Count how many times a ComboBox item was selected in C++ Builder


I need your help to resolve my problem.

I have a ComboBox with 2 items: StackExchange, and StackOverflow.

I want to know how to count of how many times StackOverflow item was selected and put the results in a messagebox.

Thanks and Regards.


Solution

  • Create an array of two int values, initialized to 0s. In the TComboBox::OnSelect event, increment the appropriate array item based on the index reported by the TComboBox::ItemIndex property.

    private:
        int Counters[2];
    
    void __fastcall TMyForm::ComboBox1Select(TOBJECT *Sender)
    {
        Counters[ComboBox1->ItemIndex]++;
    }
    
    void __fastcall TMyForm::DisplayCounts()
    {
        String Msg;
        Msg.sprintf(_D("%s: %d\n%s: %d"),
            ComboBox1->Items->Strings[0].c_str(), Counters[0],
            ComboBox1->Items->Strings[1].c_str(), Counters[1]
        );
        ShowMessage(Msg);
    }