Click here to Skip to main content
Click here to Skip to main content
Go to top

Read Only ComboBox

, 3 Oct 2000
Rate this:
Please Sign up or sign in to vote.
Show a disabled dropdown style combobox like a read only edit box.
<!-- Link to source file download -->
  • Download source files - 1 Kb
  • <!-- Article image -->

    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

    Share

    About the Author

    Tim McColl
    Software Developer (Senior)
    Australia Australia
    No Biography provided

    Comments and Discussions

     
    Generalread only combo boxes Pinmemberodesanmi16-Jan-10 14:48 
    GeneralUnselectable item in Combo Box - MFC PinmemberAnanyaMohan4-Jul-08 3:45 
    QuestionNice idea, but a small problem. Pinmemberharrys123418-Jan-08 9:59 
    When trying to use the code I'm getting an assertion error when trying to enable the pEdit control. It seems that GetWindow(GW_CHILD) does not return a valid CEdit control (it returns NULL). Any idea what might be the problem?
    NewsRe: Nice idea, but a small problem. Pinmemberharrys123418-Jan-08 13:19 
    GeneralRe: Nice idea, but a small problem. Pinmemberyasan_v323-Jul-08 9:55 
    GeneralRe: Nice idea, but a small problem. PinmemberRobin Imrie21-Oct-09 4:08 
    GeneralGood article! PinmemberLarpyboy26-Aug-07 14:56 
    QuestionCan Scroll PinmemberLeMehmet8-Dec-00 1:28 
    GeneralFor ATL and or WTL PinsussRodger Bernstein8-Oct-00 7:53 

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

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

    | Advertise | Privacy | Mobile
    Web02 | 2.8.140916.1 | Last Updated 4 Oct 2000
    Article Copyright 2000 by Tim McColl
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid