Introduction
Editable MAC address fields are necessary for anyone configuring a virtual network. Since the masked text box is primitive in VS2005, I decided to create a MAC address text box of my own. This is a text box that allows a user to input a MAC address, along with a class for handling the text box.
About the code
It is very similar to the IP TextBox article posted by mawnkay, as his efforts to creating an easy to use IP text box inspired me. All it is, is six edit boxes and five static ':' text boxes between them.
Since it seems pointless just to give out a textbox that only returns a string, I have included a class to use with the text box so you may set and save the address programmatically. The code details follow.
Developers
Using the text box
Just drag and drop the DLL from the MACTools\matb\matb\bin\release directory in to your designer toolbox in Visual Studio 2005 (I put it under the "All Windows Forms" tab) and Visual Studio will automatically add it to your list of controls. You may also add the project to your existing solution. Then, when you rebuild the solution, it will be available automatically in your toolbox at the very top (above "All Windows Forms").
Next, just use it like you would any of the standard Windows Forms controls in the design editor, by dragging it to your dialog. When you drag the control into your dialog, it will automatically make it the correct size. This box does not support resizing.
Overridden properties include:
The BackColor
and ForeColor
may be edited in the designer properties window to match the other controls in your dialog.
Using the MAC address class (MacAddr)
The class consists of six bytes, each representing the six boxes of the text box. All you have to do is use the Address
property of the MacAddr
class and the TextBox
's Text
property interchangeably. Example:
matb macTextBox = new matb();
MacAddr macVariable = new MacAddr();
macVariable.Address = macTextBox.Text;
macTextBox.Text = macVariable.Address;
You may edit the bytes of the MAC address individually, by using the properties of each byte:
macVariable.FirstByte = 0x1a;
macVariable.SecondByte = 0x2b;
macVariable.ThirdByte = 0x3c;
macVariable.FourthByte = 0x4d;
macVariable.FifthByte = 0x5e;
macVariable.SixthByte = 0x6f;
Here is a small code walkthrough on what is happening behind the scenes. Here are the private variables, each representing the respective box in the TextBox
control:
private byte firstByte;
private byte secondByte;
private byte thirdByte;
private byte fourthByte;
private byte fifthByte;
private byte sixthByte;
Now, the tricky part. I had to devise a way to convert a string to a byte and vice versa. The hardest part was taking two letters from the string, such as "1A", and converting that into only one byte (since logically that is what it is). Here is the property the class uses to convert between the two.
public string Address
{
get
{
string temp;
temp = string.Format("{0:x2}:{1:x2}:{2:x2}:{3:x2}:{4:x2}:{5:x2}",
firstByte, secondByte, thirdByte,
fourthByte, fifthByte, sixthByte);
return temp;
}
set
{
string[] temp;
byte[] byTemp;
char[] sep = new char[1];
sep[0] = ':';
temp = value.Split(sep, 6);
byTemp = FormatByte(temp);
firstByte = byTemp[0];
secondByte = byTemp[1];
thirdByte = byTemp[2];
fourthByte = byTemp[3];
fifthByte = byTemp[4];
sixthByte = byTemp[5];
}
}
The line:
byTemp = FormatByte(temp);
actually does the dirty work. It takes a string
array and converts it into a byte
array. It isn't the best way to do it, but it works.
private byte[] FormatByte(string[] sTemp)
{
byte[] sRetValue = new byte[6];
char[] cAtoF = { 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f' };
char[] c0to9 = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 12; j++)
{
if (j < 10)
sTemp[i] = new string(sTemp[i].Replace(c0to9[j],
(char)((int)c0to9[j] - 48)).ToCharArray());
if( j < 6 )
sTemp[i] = new string(sTemp[i].Replace(cAtoF[j],
(char)((int)cAtoF[j] - 55)).ToCharArray());
else
sTemp[i] = new string(sTemp[i].Replace(cAtoF[j],
(char)((int)cAtoF[j] - 87)).ToCharArray());
}
}
for (int i = 0; i < 6; i++)
{
sRetValue[i] = (byte)(sTemp[i][0] << 4 | sTemp[i][1]);
}
return sRetValue;
}
That is pretty much it. Now, you have a class at your disposal if you want to save the user input of the MAC address text box.
Source code is included, so if anyone comes across a better way of doing something, or adds another section to the code, let me know, as I will be eager to make the change myself.
End-user
MAC address text box
Only allows a user to type a valid hexadecimal character, case insensitive. Note, this doesn't really do any error checking, since it only allows a user to enter a valid hexadecimal value. Automatically sets focus to next box once two hex values are typed by the user in each box. ':' and '-' also move to the next box. Pads zeros to the left when any of the five sections lose focus.
Future recommendations
May want to force every character to be uppercase when user types in the box. Also, support for data-binding might be a good idea (MacAddr.Address
property, and the Text
property of the TextBox
control).
History
- 16/08/2006 17:42 CST: Fixed bug regarding the
Text
property returning a ":::::" string on an unedited text box.
- 10/08/2006 12:44 CST: Added the
BackColor
and ForeColor
properties to the text box control. Included a small example of code use.
- 07/06/2006 13:41 CST: Added the
MacAddr
class for handling the text box programmatically.
- 21/05/2006 01:07 CST: Added zero-padding to edit boxes when the control loses focus.
- 14/05/2006 17:05 CST: Fixed bug regarding missing characters when two capital letters are typed in to the box. Also added default zeros to be added on initial instance.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.