0

I am trying to create a CCombobox with the following code:

    CComboBox* cSearchBar = new CComboBox();
if (!cSearchBar->Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | CBS_OWNERDRAWFIXED | CBS_DROPDOWN | CBS_AUTOHSCROLL,  CRect(150,10,325,15), this, IDC_COMBO))
    TRACE0("Failed to create search bar\n");

But it gives an error message of "Debug Assertion Failed" on

File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\winctrl1.cpp, Line 271 This problem only occurs when I try to create the CCombobox with CBS_OWNERDRAWFIXED flag. Someone please tell me how to create a CCombobox programmatically with Owner Drawn property set to fixed.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
  • The debug assertion evaluates an expression. That should give you a good hint as to what went wrong. (Note: You have the source code.) – IInspectable Jul 07 '15 at 07:00

1 Answers1

0

To use ownerdraw control you have to use your own class.

class CMyComboBox : public CComboBox
{
public:
    void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    void MeasureItem(LPMEASUREITEMSTRUCT ms);//ms->itemHeight = 15...
};

Example: https://msdn.microsoft.com/en-us/library/y5hb5f9t.aspx

Or you can use a regular combobox and just change its font. Declare font as class member. Create the font and call SetFont after ComboBox is created:

{
    CFont m_font;
    //...
}

cSearchBar->Create...
m_font.CreatePointFont(120, L"Segoe UI");
cSearchBar->SetFont(&m_font);
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • It doesn't stay the same. The previous error was most likely due to missing `MeasureItem`. On your computer the file is probably sitting here: `C:\Program Files (x86)\Microsoft Visual Studio VERSION\VC\atlmfc\src\mfc\winctrl1.cpp`, you can check it as was suggested in other comment. – Barmak Shemirani Jul 07 '15 at 07:47
  • It remained the same in my case, maybe I'm not following the right way... Well, you also suggested me to use a regular combobox and change its font, can you please elaborate how to do it? – Itban Saeed Jul 07 '15 at 08:37
  • I updated the answer for font option. Make sure font is not declared as local variable. Font has to be declared as class member, or global. – Barmak Shemirani Jul 07 '15 at 08:47
  • I'm still facing the same problem of "Debug Assertion Failed" on the same line. – Itban Saeed Jul 07 '15 at 10:09
  • Then remove `CBS_OWNERDRAWFIXED` flag and create a regular combobox – Barmak Shemirani Jul 07 '15 at 16:14
  • In my project, all the controls are being created dynamically so I can not create a regular combobox. I need to create it dynamically. – Itban Saeed Jul 08 '15 at 05:54
  • There shouldn't be a problem if you remove `CBS_OWNERDRAWFIXED`. You wrote earlier that the problem occurs only when you add `CBS_OWNERDRAWFIXED`. If you add `CBS_OWNERDRAWFIXED` for `CComboBox` without driving owner draw control then the program will crash. – Barmak Shemirani Jul 08 '15 at 07:10
  • I can not remove CBS_OWNERDRAWFIXED. I am using a custom control CGroupComboBox that is derived from CComboBox for which I need CBS_OWNERDRAWFIXED flag to be set, neither I can leave this flag nor I can use regular control. I must use the derived custom control using the CBS_OWNERDRAWFIXED flag. – Itban Saeed Jul 08 '15 at 09:54