Dynamically Changing CComboBox From Dropdown To Droplist
-
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);
-
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);
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 :-)
-
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 :-)
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
-
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);
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
-
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
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?