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