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. Dynamically Changing CComboBox From Dropdown To Droplist

Dynamically Changing CComboBox From Dropdown To Droplist

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlearning
5 Posts 4 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.
  • J Offline
    J Offline
    John Clump
    wrote on last edited by
    #1

    Hi everyone. I have a dialog with 2 combo boxes, one is for setting what kind of data which will be put into the second combo box. For example, "String" will set the data box into a dropdown were the user can type in a string, or "boolean", which would set the data box into a drop list (where there are two entires, "Yes" or "No". I want to be able to do this dynamically, as the user selects either "String" or "boolean". I tried the following to set the data combobox from a dropdown to a drop list, with the dropdown already successfully created by default in the resource editor. When this code is run, the combo box font changed, the size of the box changed, and I could not set the drop down width to anything, and the width would be zero. The combobox did change to droplist though. Is it possible to change the combobox from sropdown to drop list dynamically? Is there a correct way to do it, so I don't have to use an extra combobox? Thanks in advance for any help! // pFrom is a pointer to a CComboBox control CRect rect; pFrom->GetWindowRect(rect); ScreenToClient(rect); pFrom->DestroyWindow(); pFrom->Create(CBS_DROPDOWNLIST, rect, this, IDC_CBO_DATA); pFrom->AddString("Yes"); pFrom->AddString("No"); pFrom->SetCurSel(0); pFrom->ShowWindow(TRUE); pFrom->EnableWindow(TRUE); pFrom->SetDroppedWidth(70);

    N P 2 Replies Last reply
    0
    • J John Clump

      Hi everyone. I have a dialog with 2 combo boxes, one is for setting what kind of data which will be put into the second combo box. For example, "String" will set the data box into a dropdown were the user can type in a string, or "boolean", which would set the data box into a drop list (where there are two entires, "Yes" or "No". I want to be able to do this dynamically, as the user selects either "String" or "boolean". I tried the following to set the data combobox from a dropdown to a drop list, with the dropdown already successfully created by default in the resource editor. When this code is run, the combo box font changed, the size of the box changed, and I could not set the drop down width to anything, and the width would be zero. The combobox did change to droplist though. Is it possible to change the combobox from sropdown to drop list dynamically? Is there a correct way to do it, so I don't have to use an extra combobox? Thanks in advance for any help! // pFrom is a pointer to a CComboBox control CRect rect; pFrom->GetWindowRect(rect); ScreenToClient(rect); pFrom->DestroyWindow(); pFrom->Create(CBS_DROPDOWNLIST, rect, this, IDC_CBO_DATA); pFrom->AddString("Yes"); pFrom->AddString("No"); pFrom->SetCurSel(0); pFrom->ShowWindow(TRUE); pFrom->EnableWindow(TRUE); pFrom->SetDroppedWidth(70);

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Unfortunately some styles are not changeable once the control has been created. This is one of them. Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Buy it, read it and admire me :-)

      S 1 Reply Last reply
      0
      • N Nish Nishant

        Unfortunately some styles are not changeable once the control has been created. This is one of them. Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Buy it, read it and admire me :-)

        S Offline
        S Offline
        srinivas vaithianathan
        wrote on last edited by
        #3

        if you cannot change the style workaround is have to two controls drowndown and droplist in the same place in the dialog and show the one you need

        1 Reply Last reply
        0
        • J John Clump

          Hi everyone. I have a dialog with 2 combo boxes, one is for setting what kind of data which will be put into the second combo box. For example, "String" will set the data box into a dropdown were the user can type in a string, or "boolean", which would set the data box into a drop list (where there are two entires, "Yes" or "No". I want to be able to do this dynamically, as the user selects either "String" or "boolean". I tried the following to set the data combobox from a dropdown to a drop list, with the dropdown already successfully created by default in the resource editor. When this code is run, the combo box font changed, the size of the box changed, and I could not set the drop down width to anything, and the width would be zero. The combobox did change to droplist though. Is it possible to change the combobox from sropdown to drop list dynamically? Is there a correct way to do it, so I don't have to use an extra combobox? Thanks in advance for any help! // pFrom is a pointer to a CComboBox control CRect rect; pFrom->GetWindowRect(rect); ScreenToClient(rect); pFrom->DestroyWindow(); pFrom->Create(CBS_DROPDOWNLIST, rect, this, IDC_CBO_DATA); pFrom->AddString("Yes"); pFrom->AddString("No"); pFrom->SetCurSel(0); pFrom->ShowWindow(TRUE); pFrom->EnableWindow(TRUE); pFrom->SetDroppedWidth(70);

          P Offline
          P Offline
          Phil J Pearson
          wrote on last edited by
          #4

          You cannot change this style at runtime. You have the right idea: delete the combobox and create another with the appropriate style. First save all the relevant attributes of the original combo and stuff them into the new one when it's ready. I used the following code to change from DROPDOWN to DROPLIST:

          // save all the relevant attributes of the existing combo, then destroy it and
          // create a new one with CBS_DROPDOWNLIST style.
          // (ModifyStyle(CBS_DROPDOWN, CBS_DROPDOWNLIST) won't work)
          DWORD dwStyle = m_Combo.GetStyle();
          DWORD dwExStyle = m_Combo.GetExStyle();
          int nID = m_Combo.GetDlgCtrlID();
          CWnd *pPrevWindow = m_Combo.GetNextWindow(GW_HWNDPREV);
          CWnd *pParentWnd = m_Combo.GetParent();
          CFont *pFont = m_Combo.GetFont();
          m_Combo.GetWindowRect(rect);
          pParentWnd->ScreenToClient(rect);
          m_Combo.DestroyWindow();
          dwStyle &= ~CBS_DROPDOWN;
          dwStyle |= CBS_DROPDOWNLIST;
          m_Combo.CreateEx(dwExStyle, "COMBOBOX", "", dwStyle, rect, pParentWnd, nID);
          m_Combo.SetFont(pFont, FALSE);
          m_Combo.SetWindowPos(pPrevWindow, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);

          Hope this helps, Regards, Phil

          J 1 Reply Last reply
          0
          • P Phil J Pearson

            You cannot change this style at runtime. You have the right idea: delete the combobox and create another with the appropriate style. First save all the relevant attributes of the original combo and stuff them into the new one when it's ready. I used the following code to change from DROPDOWN to DROPLIST:

            // save all the relevant attributes of the existing combo, then destroy it and
            // create a new one with CBS_DROPDOWNLIST style.
            // (ModifyStyle(CBS_DROPDOWN, CBS_DROPDOWNLIST) won't work)
            DWORD dwStyle = m_Combo.GetStyle();
            DWORD dwExStyle = m_Combo.GetExStyle();
            int nID = m_Combo.GetDlgCtrlID();
            CWnd *pPrevWindow = m_Combo.GetNextWindow(GW_HWNDPREV);
            CWnd *pParentWnd = m_Combo.GetParent();
            CFont *pFont = m_Combo.GetFont();
            m_Combo.GetWindowRect(rect);
            pParentWnd->ScreenToClient(rect);
            m_Combo.DestroyWindow();
            dwStyle &= ~CBS_DROPDOWN;
            dwStyle |= CBS_DROPDOWNLIST;
            m_Combo.CreateEx(dwExStyle, "COMBOBOX", "", dwStyle, rect, pParentWnd, nID);
            m_Combo.SetFont(pFont, FALSE);
            m_Combo.SetWindowPos(pPrevWindow, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);

            Hope this helps, Regards, Phil

            J Offline
            J Offline
            John Clump
            wrote on last edited by
            #5

            Thanks! Your code worked great, except the dropdown height was not preserved, and I could not change it via SetDroppedWidth(). So the user could not see the "Yes" and "No" I put in the combobox. The font and such were preserved. Why couldn't I change the dropped width?

            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