Search code examples
c#.netmaskedtextbox

IP Address in a MaskedTextBox?


How can I use a MaskedTextBox to prevent the user from entering an invalid IP address? (I want it to behave just like the Windows one).


Solution

  • Try this:

    IPAddress ipAddress;
    if (IPAddress.TryParse(maskedTextBoxY.Text, out ipAddress))
    {
        //valid ip
     }
    else
     {
        //is not valid ip
    }
    

    note: to use it, you need import the System.Net namespace:

    using System.Net;