IP Address in a MaskedTextBox?

2019-01-12 00:10发布

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).

6条回答
再贱就再见
2楼-- · 2019-01-12 00:36

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;
查看更多
Ridiculous、
3楼-- · 2019-01-12 00:41

I made an IP masked textbox that mimicks the Windows one.

Has the same width, height, prevents users from entering >255 values, jumps boxes, etc, etc... If you still need it and want to try it out it's here:

https://github.com/RuvenSalamon/IP-MaskedTextBox

(I don't know if this counts as self promotion but it's open-source so I reckon it's ok.)

查看更多
太酷不给撩
4楼-- · 2019-01-12 00:43

Set the mask to: ###.###.###.###

Will display like this:

enter image description here

查看更多
贼婆χ
5楼-- · 2019-01-12 00:44

There is no complex solution for this question yet. I think @HaraldDutch answer is closest, but it is not prevet from input with space character. Using additional instruction:

IPAdressBox.ResetOnSpace = false;

generaly solved problem, but more complex is to implement own custom data typewith Parse method.

            public class IPValidator
            {
                public static IPValidator Parse(string input)
                {
                    Regex regexpr = new Regex(@" ");
                    Match match = regexpr.Match(input);
                    if (match.Success)
                        return new IPValidator();
                    else throw new ArgumentException(input);
                }
            }

Where regexpr is specific expresion to validate IP. After that it can be use as ValidatingType:

 IPAdressBox.ValidatingType = typeof(IPValidator);
查看更多
别忘想泡老子
6楼-- · 2019-01-12 00:45

Much simpler than the other answers:

Use System.Net.IPAddress and System.Windows.Forms.MaskedTextBox

set the following properties of the MaskedTextBox:

MaskedTextBox.Mask = ###.###.###.###
MaskedTextBox.ValidatingType = typeof(System.Net.IPAddress);

whenever the text box is validating, event MaskedTextBox.TypeValidationCompleted is raised. The event arguments show the following:

  • Is the typed text acceptable as an IP address? (= does System.Net.IPAddress.TryParse return ok)
  • Description of the error as a string
  • The parsed value (= an object of System.NET.IpAddress
  • The type of the parsed value. Needed if you have several MaskedTextBoxes with different masks

Upon receipt of this event you can decide whether to use the value or notify the user what is wrong with the value.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-12 00:58

It is better to use REGEX to validate user input. Here's an example:

         string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";

        string ip1 = "192.168.0.1";
        string ip2 = "302.0.0.1";

        Regex.IsMatch(ip1, pattern); // returns true
        Regex.IsMatch(ip2, pattern); // returns false
查看更多
登录 后发表回答