jonsb
jonsb

Reputation: 2064

Iterate through the items of an MFC CComboBox

I need to iterate through the items (strings) in a CComboBox to check which string is the longest. How can I get each item in the list?

Upvotes: 4

Views: 5951

Answers (1)

Adam Pierce
Adam Pierce

Reputation: 34365

Try the GetLBTextLen() function

Here's an example from MSDN:

// Dump all of the items in the combo box.
   CString str, str2;
   int n;
   for (int i=0;i < pmyComboBox->GetCount();i++)
   {
      n = pmyComboBox->GetLBTextLen( i );
      pmyComboBox->GetLBText( i, str.GetBuffer(n) );
      str.ReleaseBuffer();

      str2.Format(_T("item %d: %s\r\n"), i, str.GetBuffer(0));
      afxDump << str2;
   }

Upvotes: 7

Related Questions