Reputation: 87
I need a Maskedtextbox/Textbox which one does accept versions just like : Exaple: X.X.X.X or X.X.X.XX or X.XX.X.X and so on (X = a digit)
Is there a possibility that i can say no alphabetical/symbol-inputs, it must have 4 points, and it cant be null. (probably i need a Textbox insteat of a Manskedtextbox)
I set the mask of my Maskedtextbox under Properties --> Mask
Current Mask = 9.9.9.9 Currently my Maskedtextbox does accept only X.X.X.X
Please correct me :) I'm learning so every constructive criticism is welcome :)
Upvotes: 0
Views: 173
Reputation: 6562
You could do something like this or use RegEx I'm sure
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
bool handled = false;
//test if digit
if (char.IsDigit(e.KeyChar))
{
//only allow 2 digits
//test if length is long enough to contain a prior char
if (textBox1.Text.Length - 1 >= 0)
{
//test if prior char is digit
if (char.IsDigit(textBox1.Text[textBox1.Text.Length -1]))
{
//test if length is long enough to have 2nd prior char
if (textBox1.Text.Length -2 >= 0)
{
//test 2 prior char if it's a digit because you
//only want to allow 2 not 3
if (char.IsDigit(textBox1.Text[textBox1.Text.Length - 2]))
{
handled = true;
}
}
}
}
}
//test if decimal
else if (e.KeyChar.Equals('.'))
{
//only allow 4 decimals
if (textBox1.Text.Count(t => t.Equals('.')).Equals(3))
{
handled = true;
}
else
{
//don't allow multiple decimals next to each other
//test if we have prior char
if (textBox1.Text.Length - 1 >= 0)
{
//test if prior char is decimal
if (textBox1.Text[textBox1.Text.Length - 1].Equals('.'))
{
handled = true;
}
}
}
}
else
{
handled = true;
}
e.Handled = handled;
}
Hooking up events from the designer ...
Upvotes: 1