Read Only ComboBox






4.53/5 (10 votes)
Aug 7, 2000

124021

1576
Show a disabled dropdown style combobox like a read only edit box.
Introduction
This is a very quick fix to displaying a disabled combo box as read only. If you have a form with lots of read only edit boxes, the normal disabled combo box looks a bit funny. Read only edit boxes have black text with grey backgrounds. While disabled combo boxes have dark grey text with grey backgrounds. Hard to explain to users.
This code only works for drop down style combo boxes. This is because I am setting the child edit box as enabled and read only when the parent combo box is disabled.
When the combo box gets a WM_ENABLE
message, check the bEnable
flag and call EnableWindow
and SetReadOnly
for the edit box.
A pointer to the edit box is retrieved by just getting the first child window of the
combo box, GetWindow(GW_CHILD)
.
BEGIN_MESSAGE_MAP(CReadOnlyComboBox, CComboBox) ... ON_WM_ENABLE() ... END_MESSAGE_MAP() void CReadOnlyComboBox::OnEnable(BOOL bEnable) { CComboBox::OnEnable(bEnable); // Get edit control which happens to be the first child window CEdit* pEdit = (CEdit*)GetWindow(GW_CHILD); // Always have the edit box enabled pEdit->EnableWindow(TRUE); // Set read only is combo box is disabled pEdit->SetReadOnly(!bEnable); }
The only down side to this approach is the text can't be selected with the mouse like a read only edit box.