C # - Limit value in TextBox

0

Hello, I'm using C #, and I'm having some problems using a TextBox. I have a TextBox with this format: 192.168.100.1, to be more specific I am putting IP numbers.

Iwouldlikeitfromthesecond.(100),thenumberwasnotgreaterthan255

I've tried using Substring and Length, but the program's error. Can anyone help me ???

    
asked by anonymous 29.12.2016 / 22:35

1 answer

0

Tip 1

I recommend that you use the NumericUpDown component. Include four of them in your form and seven properties% with% to 0 and% with% to 255.

The advantage of using this type of component is that you have control over the numbers you are going to receive, plus you are sure to only receive numbers.

Tip 2

Use the minimum component. Change the property maximum to '999,999,999,999' and the property MaskedTextBox to mask .

To check the value you enter, use the function:

    public static int verificaValor(string ip, int parte)
    {
//ip é o texto com o ip
//parte é qual das quatro partes você quer verificar
        if (parte == 1)
            return int.Parse(ip.Substring(0, 3));
        if (parte == 2)
            return int.Parse(ip.Substring(4, 3));
        if (parte == 3)
            return int.Parse(ip.Substring(8, 3));
        if (parte == 4)
            return int.Parse(ip.Substring(12, 3));
    }

This function will return the value of one of the 4 parts of ip, eg:

If the ip is SkipLiterals and you use false , the value 100 will be returned.

So you can check the value using an if:

if (verificaValor(textBoxIp.text, 3) > 255)
    //mensagem de erro

Even so, I suggest the first way to do it, because it prevents the user from entering values above what you want.

    
30.12.2016 / 00:05