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

MAC address edit control

, 16 Feb 2007
Rate this:
Please Sign up or sign in to vote.
CMACAddrEdit – MAC address edit control

Sample image

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:

  1. Add the files MACAddrEdit.cpp and MACAddrEdit.h to your project.
  2. In the Resource Editor of Visual Studio, place a normal edit control onto the dialog.
  3. Right-click on the edit control and select Class Wizard.
  4. Create a new member variable for the control of type CEdit.
  5. Include the CMACAddrEdit.h to your dialog header file.
  6. 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);                  // 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

perle1
Systems Engineer
Germany Germany
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.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberRadspeiche7-Dec-11 4:57 
QuestionHow to make this control support multiline MAC entry? [modified] PinmemberNDAO28-Dec-07 8:30 
AnswerRe: How to make this control support multiline MAC entry? Pinmemberperle13-Jan-08 5:17 
GeneralUseful PinmemberVolker Thieme3-Feb-07 21:50 
GeneralRe: Useful Pinmemberperle14-Feb-07 6:17 
GeneralVery nice PinmemberRob Caldecott3-Feb-07 5:18 
GeneralRe: Very nice Pinmemberperle13-Feb-07 11:14 

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
Web01 | 2.8.140827.1 | Last Updated 16 Feb 2007
Article Copyright 2007 by perle1
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid