Click here to Skip to main content
15,886,061 members
Articles / Desktop Programming / MFC
Article

Lockable Edit and ComboBox Controls

Rate me:
Please Sign up or sign in to vote.
3.94/5 (7 votes)
2 Dec 20042 min read 45.6K   1.4K   16   1
An article describing how to make the CEdit and CComboBox classes better for enabling/disabling.

Lockable Controls

Introduction

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

Background

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)
{
    if(!m_bEnabled)
        return;

    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
    if(!m_cboCategories.GetEnabled())
        return;

    //just put text selected in combobox into the edit control
    CString strText;
    m_cboCategories.GetLBText(m_cboCategories.GetCurSel(),strText);
    m_txtNotes.SetWindowText(strText);
}

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

History

  • Posted 12/01/2004

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
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.

Comments and Discussions

 
GeneralPer design Pin
Christian Rytter10-Dec-04 2:33
Christian Rytter10-Dec-04 2:33 

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.