Visual Studio .NET Controls for Taking IP Address and MAC Address Info as Inputs


/ Published in: C#
Save to your folder(s)

This snippet builds on my last snippet regarding [User input validation in Visual Studio .NET using MaskedTextBoxes and an ErrorProvider](http://snipplr.com/view/22419/user-input-validation-in-visual-studio-net-using-maskedtextboxes-and-an-errorprovider/).

Using a MaskedTextBox (MTB) works well for a MAC address because the length of the input is always the same: six octets (a.k.a. bytes) with hexadecimal input (0-9A-F) with or without colon (:) or dash (-) separators. You can set the Mask to be ">aa\:aa\:aa\:aa\:aa\:aa", which will make all characters typed in go to uppercase ('>'), accept, optionally, two alphanumeric characters ('aa') followed by a literal colon ('\:'), and so on. (The reason for the literal colon is so that it isn't translated to a different Time separator character.)

Unfortunately, I don't think there is a way to specify that the separator can be either a dash *or* a colon, but, in my test, pasting in input with either separator (or none) works ok (i.e. "A0-b1-C2-d3-E4-f5", "A0:b1:C2:d3:E4:f5", or "A0b1C2d3E4f5"). Another limitation is that the input is optional, but that can be compensated for in your own validation code. That code should start by assigning the ValidatingType of your MTB to typeof(Mac). (Of course, you need to create a Mac class for doing MAC address manipulation, since .NET doesn't include one for you.) [Edit: .NET does have a built-in MAC address class: System.Net.NetworkInformation.PhysicalAddress] Look up the MSDN for ValidatingType to know what all the class needs to support for this functionality.

For the IP address, things are a little easier but a little worse. The MTB doesn't appear to support variable-length input (e.g. "1" as well as "192"), so we can't use the mask to our advantage. (If you don't believe me, try a Mask of "990\.990\.990\.990" and see.) So our UI isn't as nice, but oh well. For validation, set the ValidatingType to typeof(System.Net.IPAddress) and cast your e.ReturnValue in your TypeValidationCompleted event handler to that same type.

That's it. A bit of a cursory explanation, but you should be able to piece together the rest. Leave questions if you have 'em.


Copy this code and paste it in your HTML
  1. private void PrimaryForm_Load(object sender, EventArgs e)
  2. {
  3. maskedTextBox1.ValidatingType = typeof (Mac);
  4. maskedTextBox1.TypeValidationCompleted +=
  5. new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
  6.  
  7. w_IPIOMasked.ValidatingType = typeof (IPAddress);
  8. w_IPIOMasked.TypeValidationCompleted += new TypeValidationEventHandler(w_IPIOMasked_TypeValidationCompleted);
  9. }
  10.  
  11. void w_IPIOMasked_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
  12. {
  13. //NOTE Occurs when the form closes, too
  14. if (e.IsValidInput)
  15. {
  16. var ip = (IPAddress)e.ReturnValue;
  17. w_errorProvider.Clear();
  18. }
  19. else
  20. {
  21. w_errorProvider.SetError((Control)sender, e.Message);
  22. }
  23. }
  24.  
  25. void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
  26. {
  27. MessageBox.Show("maskedTextBox1_TypeValidationCompleted");
  28. if (e.IsValidInput)
  29. {
  30. var mac = (Mac)e.ReturnValue;
  31. w_errorProvider.Clear();
  32. }
  33. else
  34. {
  35. w_errorProvider.SetError(maskedTextBox1, e.Message);
  36. }
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.