none
Maskedtextbox formatting for an IP address

    Question

  • Hi Everyone,

    I've been trying to format a Maskedtextbox so that I can enter an IP address. I've enterd the mast as ###.###.###.### but obviously I have to use all three digits ie 192.168.001.001 for the mask to work correctly. Is there a way I can have it so in each octect only the 1st value is required, and the other two optional.

    Also, once this is working is there a way to specifically extract parts of the IP address? Ie if a class C is entered, is there a way to extract only the values in the last octect.

    Thanks

    David
    Friday, September 12, 2008 8:26 PM

Answers

  • Believe it or not, there is a native Win32 control that supports handling of IP Addresses. All you need to do is derive a class from TextBox, but override the CreateParams property and set the ClassName to "SysIPAddress32".
    "A cat always lands feet first. Toast always lands butter side down. A cat with toast strapped on its back will hover above the air in a state of quantum indecision." ~Unknown
    Saturday, September 13, 2008 2:34 AM

All replies

  • Personally I would create a new custom control.  You can inherit from the textbox.  It would look something like this:

    public class IPTextBox : TextBox
    {
         //code omitted
    }

    Then I would override the OnLeave method to validate the text using a regular expression:

    public override OnLeave(EventArgs e)
    {
         System.Text.RegularExpressions.RegEx mReg = new System.Text.RegularExpressions.RegEx([regular expression]);
         System.Text.RegularExpressions.Match mMatch = mReg.Match(Text);
         if (!mMatch.Success)
              Clear();
         base.OnLeave(e);
    }

    Replace the [regular expression] with the following string value:

    @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"

    this RegEx validates IP addresses and was found at the following site:

    http://regexlib.com/Search.aspx?k=ip%20address

    What this will do is clear out the textbox if a valid IP address is not entered.  You can make it do other stuff instead of clearing out the box, like throwing a FormatException that you can then catch when you leave the textbox.

    There are 10 types of people in this world, those who understand Binary, and those who don't.
    Friday, September 12, 2008 8:52 PM
  •  is there a way to extract only the values in the last octect.

    Apply subnet mask 255.255.255 using bitwise &.

    192.168.001.001 & 255.255.255 will give you 001.

    AlexB
    Saturday, September 13, 2008 12:05 AM
  • anubisascends, the OP question was different. He does not want to validate the IP address. In fact his maskedtextbox will do it no questions asked. He want to make sure that the control will be happy with just a single digit entered instead of three. I think it should be, why not, provided......

    Now for the OP, I would create 3 (or four) masked controls stuck together and this will answer you plight.

    AlexB
    Saturday, September 13, 2008 12:11 AM
  • Believe it or not, there is a native Win32 control that supports handling of IP Addresses. All you need to do is derive a class from TextBox, but override the CreateParams property and set the ClassName to "SysIPAddress32".
    "A cat always lands feet first. Toast always lands butter side down. A cat with toast strapped on its back will hover above the air in a state of quantum indecision." ~Unknown
    Saturday, September 13, 2008 2:34 AM
  • It is very nice to know.
    AlexB
    Sunday, September 14, 2008 2:51 AM
  • This answer is incorrect.  There is a bug in the SysIPAddress32 class that deletes the font when it is disposed that corrupts the system font.  So in addition to overriding the CreateParams property as stated to work around the bug you also need to override the Font property and in the setter clone the font being passed in and a constructor needs to be created to ensure it uses the cloned font.

    I realize this is an old thread but with search engines today I think it's important to have the correct information marked as the answer.


    • Edited by Nick Hanson Tuesday, November 15, 2011 5:28 PM
    Tuesday, November 15, 2011 5:27 PM