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).
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Try this:
note: to use it, you need import the
System.Net
namespace: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.)
Set the mask to:
###.###.###.###
Will display like this:
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:
generaly solved problem, but more complex is to implement own custom data typewith Parse method.
Where regexpr is specific expresion to validate IP. After that it can be use as ValidatingType:
Much simpler than the other answers:
Use System.Net.IPAddress and System.Windows.Forms.MaskedTextBox
set the following properties of the MaskedTextBox:
whenever the text box is validating, event MaskedTextBox.TypeValidationCompleted is raised. The event arguments show the following:
Upon receipt of this event you can decide whether to use the value or notify the user what is wrong with the value.
It is better to use REGEX to validate user input. Here's an example: