Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Lockable Edit and ComboBox Controls

0.00/5 (No votes)
2 Dec 2004 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