sergiol
sergiol

Reputation: 4337

CComboBox FindString empty

The MFC CComboBox allows a person to do an AddString of an empty string. I just proved that by doing a GetCount() before and another after the AddString; Count was 0, then it became 1; and the GUI also seems to reflect it, as its list was an huge empty box, and when adding it became a one-liner.

I also proved it further by doing

            int a = m_combo.GetCount();
            CString sx= _T("Not empty string");
            if(a == 1)
                m_combo.GetLBText(0, sx);

            TRACE(_T("Start<%s>End"), sx);

and the Output window displays

File.cpp(9) : atlTraceGeneral - Start<>End

so we conclude the sx variable is empty.

Then I do a FindString having a CString m_name variable which is empty:

 int idx= m_combo.FindString(-1, m_name);

And it returns CB_ERR!

Is it standard behavior for empty string entries? Official documentation doesn't say anything about it!

If it is, what is the simplest way to override it? Is there some parameter or change in the resources to change the behaviour? If there is not, I am thinking about deriving or composing a class just for the case where the string is Empty!

Upvotes: 1

Views: 882

Answers (1)

sergiol
sergiol

Reputation: 4337

I did it manually for the empty string and it works!

CString sItem;
int idx_aux= CB_ERR;

// need it because FindString, FindStringExact and SelectSring return CB_ERR when we provide an empty string to them!

if(m_name.IsEmpty())
{
    for (int i=0; i<m_combo.GetCount(); i++)
    {
        m_combo.GetLBText(i, sItem);

        if(sItem.IsEmpty())
        {
            idx_aux= i;
            break;
        }
    }
}
else
    idx_aux= m_combo.FindString(-1, m_name);

Upvotes: 1

Related Questions