Read-Only ComboBoxes

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
Ok, help me out gang:

I saw some C++ code on making combo box controls read-only, as opposed to disabled (where the font is grey, or shaded out).

I want the same thing for VB.Net, but couldn't figure out on my own how to translate their code. I'm attaching the C++ code below. Maybe someone can translate it, or give me some of their own code to do this.

PS: I don't want to create another control. Maybe I'll be ready for that at another time, but I really feel like I should be able to just make these crazy combobox controls read-only. I'll even take a creative solution to this.

Example: I tried to remove all tab stops on all controls and everytime someone entered into a combobox, they automatically went to the close button. But that didn't work, as I could click and then click again into the combobox and it let me in (although I don't know why). I used GotFocus. Maybe I should've used Enter??

Anyway, here's the C++ code for making a combobox readonly as opposed to disabled. Can any one translate it to VB.NET?
=====================================
CODE AND EXPLANATION BELOW:
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);
}
 
Back
Top