Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
12 captures
25 Jan 2008 - 10 Nov 2012
MarDECApr
Previous capture20Next capture
200920112012
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20111220022641/http://www.codeproject.com:80/KB/edit/MACEditControl.aspx
Click here to Skip to main content
8,347,086 members and growing!
EmailPassword Lost password?
Home
Search within:




Licence CPOL
First Posted 2 Feb 2007
Views 29,829
Downloads 944
Bookmarked 26 times

MAC address edit control

Byperle1 | 16 Feb 2007
CMACAddrEdit – MAC address edit control
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
7
  4.76 (13 votes)

1

2
1 vote, 7.7%
3
2 votes, 15.4%
4
10 votes, 76.9%
5
4.76/5 - 13 votes
1 removed
μ 4.58, σa 1.14 [?]
Sponsored Links

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 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)

  • 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 includeCMACAddrEditControls into your project:

  1. Add the filesMACAddrEdit.cpp andMACAddrEdit.h to your project.
  2. In theResource Editor of Visual Studio, place a normal edit control onto the dialog.
  3. Right-click on the edit control and selectClass Wizard.
  4. Create a new member variable for the control of typeCEdit.
  5. Include theCMACAddrEdit.h to your dialog header file.
  6. Replace theCEditdeclaration withCMACAddrEditin 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.

License

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

About the Author

perle1

Systems Engineer

Germany Germany

Member
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.


loading...
Sign Up to vote  PoorExcellent
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 RefreshFirstPrevNext
GeneralMy vote of 5memberRadspeiche5:57 7 Dec '11  
QuestionHow to make this control support multiline MAC entry? [modified]memberNDAO9:30 28 Dec '07  
AnswerRe: How to make this control support multiline MAC entry?memberperle16:17 3 Jan '08  
GeneralUsefulmemberVolker Thieme22:50 3 Feb '07  
GeneralRe: Usefulmemberperle17:17 4 Feb '07  
GeneralVery nicememberRob Caldecott6:18 3 Feb '07  
GeneralRe: Very nicememberperle112:14 3 Feb '07  
Last Visit: 19:00 31 Dec '99     Last Update: 16:26 19 Dec '111

General General   News News   Suggestion Suggestion   Question Question   Bug Bug   Answer Answer   Joke Joke   Rant Rant   Admin Admin   

Permalink |Advertise |Privacy |Mobile
Web04 |2.5.111208.1 |Last Updated 16 Feb 2007
Article Copyright 2007 by perle1
Everything elseCopyright ©CodeProject, 1999-2011
Terms of Use
Layout:fixed|fluid

The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp