Click here to Skip to main content
13,249,512 members (35,507 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 2 Dec 2004

Lockable Edit and ComboBox Controls

, 2 Dec 2004
Rate this:
Please Sign up or sign in to vote.
An article describing how to make the CEdit and CComboBox classes better for enabling/disabling.

Lockable Controls


This article has two classes derived from CEdit and CCombobox that allow them to be locked without appearing disabled.


One of the things I like about using some of the controls in VB is that they have a 'locked' property. Using MFC, there's no inherent property equivalent to this, so I decided to make my own. I don't like the way the controls look (edit and combobox) when they're disabled using MFC.

Using the code

To use this code, just add the LockEdit.h, LockCombo.h and their respective .cpp files to your project. I recently found that if I add these classes to my project first, when using the ClassWizard to add a variable for these classes, I can just type in 'CLockCombo' or 'CLockEdit' instead of using the MFC classes and then going into the code and changing it later.

Both classes use a member variable 'm_bEnabled' to hold the current state of the control. The code uses this variable to see whether or not further processing of a message should be done. For instance, here's the 'OnChar()' method of CLockEdit:

void CLockEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

    CEdit::OnChar(nChar, nRepCnt, nFlags);

The CLockCombo class also has an integer member variable to hold the index of the current item that it gets when the list is dropped down. If the control is disabled, it just sets it back to the previous item when the list is closed. Also, the CLockEdit class allows for cut, copy, and paste using the normal Windows shortcut keys (CTRL+C, CTRL+X, CTRL+V). And since the combo isn't disabled, it allows users to see what the other choices in the combo are, even though they can't select anything. The code for the RecalcDropWidth() function was pilfered shamelessly from a project of Chris Maunder that I used a while back.

Points of Interest

The only problem that I see as of right now is that if you're handling the OnSelChange() event, you have to make sure that the combobox is enabled before you do any processing. This is seen in the following snippet:

void CLockedControlsDlg::OnCbnSelchangeCombo1()
    //if combo is not currently enabled, don't do any more handling of the event

    //just put text selected in combobox into the edit control
    CString strText;

Feel free to use this code in any way you like. Any additions and/or changes are welcome.


  • Posted 12/01/2004


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


About the Author

Web Developer
United States United States
Started off with vb6 Smile | :) and am self-taught at C++, FoxPro, DirectX, etc. I'm currently developing with VFP9 and C# in desktop apps, webservices, telephony apps.

You may also be interested in...


Comments and Discussions

GeneralPer design Pin
Christian Rytter10-Dec-04 3:33
memberChristian Rytter10-Dec-04 3:33 
The the solution this article discusse, is something that always annoyed me in VB Smile | :)

The fact that an edit control "looks" disabled is per design and I think it is good. It enables the user to:

1: understand why the edit box dosnt say anything, when something is attempted to be written there.
2: easily get an overview of the edit fields of interest. E.g. Imagine a dialogue with 20 edit fields, of which 3 is some I can actually configure, due to some other limits. If I can see the difference, it is very easy for me to discard the remaining 17, as something I need to understand.
3: recognize the standard that you see in all Microsoft programs and most others. Personally, this is the reason why I never have to read a manual - I can guess my way through some software user interface. (Yes, I'm aware that the example is a bit extreme here, but speaking in general)

Otherwise your code seems fair, although not very innovative Smile | :)

Best Regards,

Christian Rytter

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 3 Dec 2004
Article Copyright 2004 by BlackDice
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid