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)
CString CMACAddrEdit::GetCurrentMask()
BOOL CMACAddrEdit::SetValidEditChars(CUIntArray* arChars)
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 with CMACAddrEdit
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); GetSel(iPos, iEndPos);
nChar = _totupper(nChar);
if (nChar == VK_BACK) {
if (iEndPos == csMACText.GetLength()) { if ((iPos == iEndPos) && (iPos != 0))
iPos--; csMACText.Delete(iPos, iEndPos - iPos); }
else {
if ((iPos == iEndPos) && (iPos != 0)) iPos--; for (int i = iPos; i < iEndPos; i++)
{ if ((tcChar = m_MACMask[i]) == '#')
csMACText.SetAt(i, (TCHAR) '0');
}
}
SetWindowText(csMACText); SetSel(iPos, iPos); }
Else
if (IsValidEditChar(nChar)) {
if (iPos < m_MACMask.GetLength()) {
if ((tcChar = m_MACMask[iPos]) != '#')
{
if (iPos < csMACText.GetLength())
csMACText.SetAt(iPos, (TCHAR)m_MACMask[iPos]);
else
csMACText += (TCHAR)m_MACMask[iPos];
iPos++; }
if ((tcChar = m_MACMask[iPos]) == '#')
{
if (iPos < csMACText.GetLength())
csMACText.SetAt(iPos, (TCHAR) nChar);
else
csMACText += (TCHAR)nChar;
iPos++; }
SetWindowText(csMACText); SetSel(iPos, iPos);
}
}
else {
if ((iPos < m_MACMask.GetLength() &&
(tcChar = m_MACMask[iPos]) == (TCHAR)nChar))
{
if (iPos < csMACText.GetLength())
csMACText.SetAt(iPos, (TCHAR)tcChar);
else
csMACText += (TCHAR)tcChar;
iPos++;
SetWindowText(csMACText); 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.
Ralph started programming in Turbo Pascal, later in Delphi. After this, he began learning C++, which is his favorite language up to now.
He is interested in almost everything that has to do with computing, his special interests are security and networking.