MAC address edit control
CMACAddrEdit – MAC address edit control
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 from CEdit
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)
-
use this function to set a new edit mask (example "##-##-##-##-##-##" or "##:##:##:##:##:##")
-
the string provided to this function must contain the "#" char and a string length greater then 2 chars
CString CMACAddrEdit::GetCurrentMask()
-
this function return the current edit mask
BOOL CMACAddrEdit::SetValidEditChars(CUIntArray* arChars)
- use this function to set a array of chars that the user is allowed to enter.
Example:void CEdMACDlg::OnBtnNewChars() { CUIntArray m_arCharList; // only allow numbers m_arCharList.Add((TCHAR) '0'); m_arCharList.Add((TCHAR) '1'); m_arCharList.Add((TCHAR) '2'); m_arCharList.Add((TCHAR) '3'); m_arCharList.Add((TCHAR) '4'); m_arCharList.Add((TCHAR) '5'); m_arCharList.Add((TCHAR) '6'); m_arCharList.Add((TCHAR) '7'); m_arCharList.Add((TCHAR) '8'); m_arCharList.Add((TCHAR) '9'); m_ED1.SetValidEditChars(&m_arCharList); m_arCharList.RemoveAll(); } ///////////////////////////////////////////////////////////////////////
Just follow these 6 steps to include CMACAddrEdit
Controls into your project:
- Add the files MACAddrEdit.cpp and MACAddrEdit.h to your project.
- In the Resource Editor of Visual Studio, place a normal edit control onto the dialog.
- Right-click on the edit control and select Class Wizard.
- Create a new member variable for the control of type
CEdit
. - Include the CMACAddrEdit.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 char
if (nChar == VK_BACK) // we will delete chars
{
if (iEndPos == csMACText.GetLength()) // we are at the end position?
{ // there is no selection
if ((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 position
for (int i = iPos; i < iEndPos; i++)
{ // override chars
if ((tcChar = m_MACMask[i]) == '#')
csMACText.SetAt(i, (TCHAR) '0');
}
}
SetWindowText(csMACText); // show the new address
SetSel(iPos, iPos); // and set the selection
}
Else
if (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 separator
if (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 char
if (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 separator
if ((iPos < m_MACMask.GetLength() &&
(tcChar = m_MACMask[iPos]) == (TCHAR)nChar))
{
// insert/add the new char
if (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.