Click here to Skip to main content
15,868,419 members
Articles / Desktop Programming / MFC
Article

Read Only ComboBox

Rate me:
Please Sign up or sign in to vote.
4.53/5 (11 votes)
3 Oct 2000 122.9K   1.6K   35   9
Show a disabled dropdown style combobox like a read only edit box.
  • Download source files - 1 Kb
  • Sample Image - readonlycombo.jpg

    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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Software Developer (Senior)
    Australia Australia
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    Generalread only combo boxes Pin
    odesanmi16-Jan-10 14:48
    odesanmi16-Jan-10 14:48 
    GeneralUnselectable item in Combo Box - MFC Pin
    Karri Mohan4-Jul-08 3:45
    Karri Mohan4-Jul-08 3:45 
    QuestionNice idea, but a small problem. Pin
    harrys123418-Jan-08 9:59
    harrys123418-Jan-08 9:59 
    NewsRe: Nice idea, but a small problem. Pin
    harrys123418-Jan-08 13:19
    harrys123418-Jan-08 13:19 
    GeneralRe: Nice idea, but a small problem. Pin
    yv323-Jul-08 9:55
    yv323-Jul-08 9:55 
    GeneralRe: Nice idea, but a small problem. Pin
    Robin Imrie21-Oct-09 4:08
    professionalRobin Imrie21-Oct-09 4:08 
    GeneralGood article! Pin
    Larpyboy26-Aug-07 14:56
    Larpyboy26-Aug-07 14:56 
    QuestionCan Scroll Pin
    8-Dec-00 1:28
    suss8-Dec-00 1:28 
    GeneralFor ATL and or WTL Pin
    Rodger Bernstein8-Oct-00 7:53
    sussRodger Bernstein8-Oct-00 7:53 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.