Link to home
Start Free TrialLog in
Avatar of hansw77041
hansw77041

asked on

CComboBox dropdown does not work

I created a control. Based upon CComboBox.

class C_ComboBox : public CComboBox
{
  rest of this code no shown for brevity.
}

Then I use it in a dialog as follows.

C_ComboBox * m_cComboS;

m_cComboS = (C_ComboBox*)new C_ComboBox;

m_cComboS->Create( WS_CHILD|WS_VISIBLE|CBS_SORT| WS_TABSTOP|WS_VSCROLL|CBS_DROPDOWN,  CRect(131,96,376,92), this, IDC_MY_COMBO);

That works  and I can add strings.

      m_cComboS->InitStorage(25, 50);

      CString test = "1234567890";

      for ( int i=0; i<25; i++)
      {
        m_cComboS->AddString(test);
      }

int NumItems = m_cComboS->GetCount();

NumItems is equal to 25. So I assume 25 strings have been added.

Now comes the problem. I can’t get the drop down to show.

When I click the “down arrow  button” next to the edit field I expected to see the list of string that got added.

That does not work.

If I handle the       ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)

void C_ComboBox::OnDropdown()
{
    int Drop = this->GetDroppedState();
    if (!Drop)
      this->ShowDropDown(TRUE);
}

Drop is always 0 and ShowDropDown(TRUE); gets called until a stack overflow happens.

What did I do wrong…
If I use a regular CComboBox directly in the dialog and set the dropdown list height in the resource editor it works just fine.  
I Created the C_ComboBox so that I could get to the Pop-up menu feature.

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Create it on the dialog with the resource editor.
With the class wizard create a member variable m_cComboS of type CComboBox
Edit the .h file and replace the CComboBox with C_ComboBox
to read

C_ComboBox m_cComboS;

Then it should be OK
Avatar of hansw77041
hansw77041

ASKER

Thanks for the help, but it does not solve the problem.

I end up with a CComboBox and the drop down list does not appear.



You should have a variable of type C_ComboBox.
Did you set the drop height of the combo in the resource editor?
This is a snippet from a header file of mine

// Dialog Data
      //{{AFX_DATA(CMyPropertyPage1)
      enum { IDD = IDD_PROPPAGE1 };
      CBusinessYearCombo      m_cmboBusinessYear;
      CArrowKeyEdit      m_ctlDate;

and this from the cpp
void CMyPropertyPage1::DoDataExchange(CDataExchange* pDX)
{
      CPropertyPage::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CMyPropertyPage1)
      DDX_Control(pDX, IDC_COMBO_BUSINESS_YEAR, m_cmboBusinessYear);
      DDX_Control(pDX, IDC_EDIT1, m_ctlDate);


The CBusinessYearCombo is based on a CComboBox, the CArrowKeyEdit on a CEdit.
Yes I can make a ComboBox the way you descirbe it here, and that works just fine, but that is not what I need to do.

I want a class so that I can add handlers for the popup menu

class C_ComboBox : public CComboBox
{
  rest of this code not shown for brevity.
}


Yes but if you have a member variable of type C_ComboBox as I described then it is a C_ComboBox with ALL of the functionality.

My CBusinessYearCombo and CArrowKeyEdit vars have functions, that I can call, over and above the normal base class functionality.

Does doing this allow the drop arrow to drop the list part? Yes or No.
I hope the answer is yes.

Then. Are you saying you can't get your popup menu?  If you are the problem is in your C_ComboBox (or elsewhere) not in the creation of the variable you have in your dialog.
Yes I know I can access the normal functionality of the combobo through such a variable.

However I need to add three extra items to the popup menu, and to do that using the standard VC++ add-in components, the pop-up needs a class.

BTW The better way is to subclass the control like this

m_cmboBusinessYear.SubclassDlgItem(IDC_COMBO_BUSINESS_YEAR,this);

Anyway that is not a solution to this problem.

I made a class that almost works, in fact the popup menu works fine.
Adding string works fine, at least it reports there are 25 items and I can get at all 25 strings so I know the control is working that far.
.
I just cannot get the list to drop down and display the 25 strings that have been added.

Adding a handler ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown) to the class blows the stack and
calling ShowDropDown(TRUE); in the handler does not work.



I don't see why you should require a ON_CONTROL_REFLECT handler.

For clarification.
If you created it in the resource editor and had a var in the .h file
CComboBox m_cComboS;
Does it fill with strings and display the drop down when you click the drop arrow (and press the F4 key if the combo has the focus).

IF yes then please remove the ON_CONTROL_REFLECT handler, set the h file declaration to C_ComboBox m_cComboS and retry.
At this point does it not display the drop list?
Yes I can create a normal CComboBox they way you describe ( and have been doing that for many years.), and can access the normal functionality of it.
Of course it displays the strings. I have no problem using a CComboBox the way you describe it.

I think I am not explaining this welll enough, let me try one more time.

To be able to add a popup menu I need a class where the VC Studio add-in can place the code for the popup menu.
When I create and class based upon the CComboBox class I can insert a popup menu and that works fine.

However I cannot get the drop down list to display.

Thanks, one is never sure of the abilities of the person asking the question hence I have tried to do it in small steps, also to help stop me missing something obvious.

Would it be a great problem for you to have the PopUp menu in the dialog rather than the combo?  (I know it separates the functionality but what does the popup menu do in terms of functionality - is it specific to the combo or for the owner of the combo..).  If not you could use the dlg instead of your C_ComboBox class.

'When I create and class based upon the CComboBox class I can insert a popup menu and that works fine. '
Do you mean you right click on the combo and it shows the popup menu? And the F4 key?
One uses the mouse the other the keyboard - is the problem just with the mouse?

Andy,
thanks for the reply.

Well I was kind of thinking it's time to make my own version of the CComboBox with popup menu so that I don't have to re-create it every time...

So to answewr your question. Yes I want the popup handling in the new class, so that everyting is in one basket and can easily be reused.

Yes the popup is the popup you get when right clicking on the ComboBox.

hansw


ASKER CERTIFIED SOLUTION
Avatar of Priyesh
Priyesh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does the drop down work if you remove the popup menu code from your C_ComboBox?