Search code examples
c++c++buildervcl

how to get ComboBox items count


I'm trying to get an combobox items count using the following code. It doesn't give an error nor the the right amount of count. I guess I have to convert int to string, but how?

ComboBox1->ItemIndex = 1;
int count = ComboBox1->Items->Count;
Edit1->Text =  "Count: " + count;

Solution

  • This line

     int count = ComboBox1->Items->Count; 
    

    returns the numnber of string items in your TComboBox. You need to check this before setting

    ComboBox1->ItemIndex = 1;
    

    as ItemIndex is used to set the selected item in the combo box and is zero counted. To convert the integer to string in Embarcadero you can use IntToStr() function

    Edit1->Text = "Count:" + IntToStr(count)
    

    You will need #include "System.hpp" to access that function