Click here to Skip to main content
Click here to Skip to main content

Lockable Edit and ComboBox Controls

By , 2 Dec 2004
Rate this:
Please Sign up or sign in to vote.

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

About the Author

BlackDice
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 PinmemberChristian Rytter10-Dec-04 2: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    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
Web04 | 2.8.140421.2 | Last Updated 3 Dec 2004
Article Copyright 2004 by BlackDice
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid