Revision: 19945
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 3, 2009 15:33 by pckujawa
Initial Code
private void PrimaryForm_Load(object sender, EventArgs e) { maskedTextBox1.ValidatingType = typeof (Mac); maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted); w_IPIOMasked.ValidatingType = typeof (IPAddress); w_IPIOMasked.TypeValidationCompleted += new TypeValidationEventHandler(w_IPIOMasked_TypeValidationCompleted); } void w_IPIOMasked_TypeValidationCompleted(object sender, TypeValidationEventArgs e) { //NOTE Occurs when the form closes, too if (e.IsValidInput) { var ip = (IPAddress)e.ReturnValue; w_errorProvider.Clear(); } else { w_errorProvider.SetError((Control)sender, e.Message); } } void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e) { MessageBox.Show("maskedTextBox1_TypeValidationCompleted"); if (e.IsValidInput) { var mac = (Mac)e.ReturnValue; w_errorProvider.Clear(); } else { w_errorProvider.SetError(maskedTextBox1, e.Message); } }
Initial URL
Initial Description
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.
Initial Title
Visual Studio .NET Controls for Taking IP Address and MAC Address Info as Inputs
Initial Tags
validation
Initial Language
C#