Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CImageList / CComboBoxEx - what's wrong?

CImageList / CComboBoxEx - what's wrong?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi I'm using this code to insert items into my CComboBoxEx with icons:

    BOOL CConfigDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    m\_Images.Create(IDB\_ICONS, 16, 1, RGB (0, 128, 128));
    m\_ctlCombo.SetImageList(&m\_Images);
    
    for(int j=0; j<8; j++)
    {
    CString string;
    string.Format (\_T ("Item %d"), j);
    
    COMBOBOXEXITEM cbei;
    cbei.mask = CBEIF\_IMAGE | CBEIF\_SELECTEDIMAGE | CBEIF\_TEXT;
    cbei.iItem = j;
    cbei.pszText = (LPTSTR) (LPCTSTR)string;
    cbei.iImage = j;
    cbei.iSelectedImage = j;
    
    m\_ctlCombo.InsertItem(&cbei);
    }
    
    return TRUE;
    

    }

    But for some reason my Combobox remains empty. Does anybody know why? thanks in advance Greg

    M 1 Reply Last reply
    0
    • L Lost User

      Hi I'm using this code to insert items into my CComboBoxEx with icons:

      BOOL CConfigDlg::OnInitDialog()
      {
      CDialog::OnInitDialog();

      m\_Images.Create(IDB\_ICONS, 16, 1, RGB (0, 128, 128));
      m\_ctlCombo.SetImageList(&m\_Images);
      
      for(int j=0; j<8; j++)
      {
      CString string;
      string.Format (\_T ("Item %d"), j);
      
      COMBOBOXEXITEM cbei;
      cbei.mask = CBEIF\_IMAGE | CBEIF\_SELECTEDIMAGE | CBEIF\_TEXT;
      cbei.iItem = j;
      cbei.pszText = (LPTSTR) (LPCTSTR)string;
      cbei.iImage = j;
      cbei.iSelectedImage = j;
      
      m\_ctlCombo.InsertItem(&cbei);
      }
      
      return TRUE;
      

      }

      But for some reason my Combobox remains empty. Does anybody know why? thanks in advance Greg

      M Offline
      M Offline
      Mike Upton
      wrote on last edited by
      #2

      That code looks correct to me, but there are a couple of other things I can think of:

      • Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
      • Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

      As a very minor point, I don't like the (LPTSTR)(LPCTSTR)string cast, because I tend to think it's bad practice to cast away the const. How about:

      cbei.pszText=string.GetBuffer(0);
      ...
      m_ctlCombo.InsertItem(&cbei);
      string.ReleaseBuffer();


      "We are the knights who say Ni" (The Knights Who Say Ni)

      L 1 Reply Last reply
      0
      • M Mike Upton

        That code looks correct to me, but there are a couple of other things I can think of:

        • Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
        • Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

        As a very minor point, I don't like the (LPTSTR)(LPCTSTR)string cast, because I tend to think it's bad practice to cast away the const. How about:

        cbei.pszText=string.GetBuffer(0);
        ...
        m_ctlCombo.InsertItem(&cbei);
        string.ReleaseBuffer();


        "We are the knights who say Ni" (The Knights Who Say Ni)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Mike Upton wrote: Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?

        void CConfigDlg::DoDataExchange(CDataExchange* pDX)
        {
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CConfigDlg)
        DDX_Control(pDX, IDC_COMBO, m_ctlCombo);
        ...

        Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

        class CConfigDlg : public CDialog
        {
        public:
        CConfigDlg(CWnd* pParent = NULL);
        //{{AFX_DATA(CConfigDlg)
        enum { IDD = IDD_CONFIG };
        CComboBoxEx m_ctlCombo;

        I don't know what's wrong :confused:

        D M 2 Replies Last reply
        0
        • L Lost User

          Mike Upton wrote: Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?

          void CConfigDlg::DoDataExchange(CDataExchange* pDX)
          {
          CDialog::DoDataExchange(pDX);
          //{{AFX_DATA_MAP(CConfigDlg)
          DDX_Control(pDX, IDC_COMBO, m_ctlCombo);
          ...

          Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

          class CConfigDlg : public CDialog
          {
          public:
          CConfigDlg(CWnd* pParent = NULL);
          //{{AFX_DATA(CConfigDlg)
          enum { IDD = IDD_CONFIG };
          CComboBoxEx m_ctlCombo;

          I don't know what's wrong :confused:

          D Offline
          D Offline
          DanielP
          wrote on last edited by
          #4

          What does InsertItem() return? suggestions: 1. you don't need invoke GetBuffer(0). you can use just Lock() and UnLock(). 2. Clear COMBOBOXEXITEM before using by ZeroMemory(). 3. set cchTextMax in COMBOBOXEXITEM

          1 Reply Last reply
          0
          • L Lost User

            Mike Upton wrote: Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?

            void CConfigDlg::DoDataExchange(CDataExchange* pDX)
            {
            CDialog::DoDataExchange(pDX);
            //{{AFX_DATA_MAP(CConfigDlg)
            DDX_Control(pDX, IDC_COMBO, m_ctlCombo);
            ...

            Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

            class CConfigDlg : public CDialog
            {
            public:
            CConfigDlg(CWnd* pParent = NULL);
            //{{AFX_DATA(CConfigDlg)
            enum { IDD = IDD_CONFIG };
            CComboBoxEx m_ctlCombo;

            I don't know what's wrong :confused:

            M Offline
            M Offline
            Mike Upton
            wrote on last edited by
            #5

            Gregor S. wrote: _Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

            class CConfigDlg : public CDialog
            {
            public:
            CConfigDlg(CWnd* pParent = NULL);
            //{{AFX_DATA(CConfigDlg)
            enum { IDD = IDD_CONFIG };
            CComboBoxEx m_ctlCombo;_
            Sorry, that's not what I meant. What I meant was, did you insert the control into the dialog resource (in the dialog resource editor) as a standard combo box or an extended combobox? I've just created a test dialog based project using the AppWizard, added an extended combo box to the dialog (and used the wizard to add a control variable for it), added a bitmap to use for the images and an image list member in the dialog, then copied and pasted your code into the OnInitDialog method and everything works perfectly.


            "We are the knights who say Ni" (The Knights Who Say Ni)

            L 1 Reply Last reply
            0
            • M Mike Upton

              Gregor S. wrote: _Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?

              class CConfigDlg : public CDialog
              {
              public:
              CConfigDlg(CWnd* pParent = NULL);
              //{{AFX_DATA(CConfigDlg)
              enum { IDD = IDD_CONFIG };
              CComboBoxEx m_ctlCombo;_
              Sorry, that's not what I meant. What I meant was, did you insert the control into the dialog resource (in the dialog resource editor) as a standard combo box or an extended combobox? I've just created a test dialog based project using the AppWizard, added an extended combo box to the dialog (and used the wizard to add a control variable for it), added a bitmap to use for the images and an image list member in the dialog, then copied and pasted your code into the OnInitDialog method and everything works perfectly.


              "We are the knights who say Ni" (The Knights Who Say Ni)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Oh, I didn't see the icon for the extended combobox in my dialog resource editor toolbar. I replaced my old comboboxes with the new ones an now everything works fine :) Thank you very much for your help! :rose:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups