
Introduction
Recently, I was working on database software which requires an input control used for MAC addresses. After searching the Web, most of what I found, was not what I needed (small, easy to use and plain). Therefore, I created the CMACAddrEdit control derived fromCEdit
for MAC address inputs.
Background
MACAddrEdit does not validate the input data. This is volitional. I needed the possibility to help the user to input MAC addresses in the correct data format and the possibility to enter incomplete MAC addresses. The last one is very useful to search in databases for a range of MAC addresses.
Since version 1.10, now the control can handle the context menu and shortcut inputs, like Copy, Cut and Paste.
Using the code
Since version 1.10, there are 3 new public function's:
BOOL CMACAddrEdit::SetCurrentMask(CString csNewMask)
CString CMACAddrEdit::GetCurrentMask()
BOOL CMACAddrEdit::SetValidEditChars(CUIntArray* arChars)
Just follow these 6 steps to includeCMACAddrEdit
Controls into your project:
- Add the filesMACAddrEdit.cpp andMACAddrEdit.h to your project.
- In theResource Editor of Visual Studio, place a normal edit control onto the dialog.
- Right-click on the edit control and selectClass Wizard.
- Create a new member variable for the control of type
CEdit
. - Include theCMACAddrEdit.h to your dialog header file.
- Replace the
CEdit
declaration withCMACAddrEdit
in your dialog header file.
The most interesting part:
void CMACAddrEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { CString csMACText; Int iPos, iEndPos; TCHAR tcChar; GetWindowText(csMACText);// get the actual text GetSel(iPos, iEndPos);// get the actual selection nChar = _totupper(nChar);// uppercase this charif (nChar == VK_BACK)// we will delete chars {if (iEndPos == csMACText.GetLength())// we are at the end position? {// there is no selectionif ((iPos == iEndPos) && (iPos !=0)) iPos--;// decrement position csMACText.Delete(iPos, iEndPos - iPos);// delete chars }else// in the middle {if ((iPos == iEndPos) && (iPos !=0))// there is no selection iPos--;// decrement positionfor (int i = iPos; i< iEndPos; i++) {// override charsif ((tcChar = m_MACMask[i]) =='#') csMACText.SetAt(i, (TCHAR)'0'); } } SetWindowText(csMACText);// show the new address SetSel(iPos, iPos);// and set the selection } Elseif (IsValidEditChar(nChar))// we will insert valid chars {if (iPos< m_MACMask.GetLength())// check max size {// mask char is a separator ?if ((tcChar = m_MACMask[iPos]) !='#') {// insert/add the separatorif (iPos< csMACText.GetLength()) csMACText.SetAt(iPos, (TCHAR)m_MACMask[iPos]);else csMACText += (TCHAR)m_MACMask[iPos]; iPos++;// increment position !!! }// mask char is not a separator ?if ((tcChar = m_MACMask[iPos]) =='#') {// insert/add the new charif (iPos< csMACText.GetLength()) csMACText.SetAt(iPos, (TCHAR) nChar);else csMACText += (TCHAR)nChar; iPos++;// increment position } SetWindowText(csMACText);// now show the new MAC address SetSel(iPos, iPos); } }else// default: handle only separator chars {// check max size and for separatorif ((iPos< m_MACMask.GetLength() && (tcChar = m_MACMask[iPos]) == (TCHAR)nChar)) {// insert/add the new charif (iPos< csMACText.GetLength()) csMACText.SetAt(iPos, (TCHAR)tcChar);else csMACText += (TCHAR)tcChar; iPos++;// increment position SetWindowText(csMACText);// and show the MAC address SetSel(iPos, iPos); } }}///////////////////////////////////////////////////////////////////////////
That's it! You don't have to call any initialization or configuration function.
Hope you'll find it useful. Please let me know about bugs and other problems if you find any.
Enjoy!
History
Version 1.1
- Added handling for the context menu and keyboard shortcut's
- Added few function's to change / get the edit mask and allowed input characters
Version 1.0
- Initial release on the CodeProject.