C # Textbox stop receiving after the comma

4

I need a textbox to stop receiving values after the comma as soon as I hit 2 houses.

For simplicity, think that the text has a border after the comma, but none before it.

Example:

Enter the number 2.99. Therefore, whenever the user tries to put a value after the 99 it should be locked. However, it should still be possible to delete, delete, and insert values before the comma.

Current code:

public static void verificarCasasPosVirgula(object sender, KeyPressEventArgs e, String Texto)
{
    if(Texto.Contains(','))
    {
        int posicaoVirgula = Texto.IndexOf(',');
        String[] array = Texto.Split(',');

        if (array[1].Length > 1) 
        {

            if ((e.KeyChar == (char)Keys.Return || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete))
            {
                e.Handled = false;
            }
            else if(posicaoVirgula > -1)
            {
                e.Handled = true;
            }
        }
    }
}
    
asked by anonymous 10.06.2016 / 17:33

4 answers

1

Most of my code was acquired in a post here, the only thing I added was a line.

private void sua_TextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {         
           if (e.KeyChar == '.' || e.KeyChar == ',')
            {
                e.KeyChar = ',';

                if (sua_TextBox1.Text.Contains(","))
                {
                    e.Handled = true;                       
                }
                sua_TextBox1.MaxLength = sua_TextBox1.TextLength +3;
            }

            else if (!char.IsNumber(e.KeyChar) && !(e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;                   
            }               
    }
    
26.09.2016 / 06:51
1

Good afternoon

Try to give a tryparse value, so it will not accept letters or two commas.

To allow the value to be cleared, check that e.KeyChar = (char)8 (backspace)

Check the position of the cursor and validate if the character is being inserted before or after the comma

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    double value = 0;
    if (!double.TryParse(textBox1.Text + e.KeyChar.ToString(), out value) && !char.IsControl(e.KeyChar))
    {
        e.Handled = true;
    }
    if (e.KeyChar == (char)8)
    {
        return;
    }


    Char separator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator[0];
    Int32 indexSeparator = textBox1.Text.IndexOf(separator);
    String[] array = textBox1.Text.Split(separator);

    if (textBox1.SelectionStart > indexSeparator)
    {
        if (array.Length == 2)
        {
            if (array[1].Length >= 2)
                e.Handled = true;
        }
    }
}

I hope I have helped!

    
10.06.2016 / 18:58
0

I was able to solve the problem, here is the solution:

    public static void verificarCasasPosVirgula(object sender, KeyPressEventArgs e, String Texto)
    {

        if(Texto.Contains(',')){
            int posicaoVirgula = Texto.IndexOf(',');
            TextBoxDVJ txt = (TextBoxDVJ)sender;
            int cursor = Texto.LastIndexOf(e.KeyChar.ToString());
            String[] array = Texto.Split(',');


        if (array[1].Length > 1) {

            if ((e.KeyChar == (char)Keys.Return || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete))
            {
                e.Handled = false;
            }
            else if(txt.SelectionStart > posicaoVirgula) //Aqui está o truque
            {
                e.Handled = true;


            }

        }

        }

    }
    
13.06.2016 / 15:47
0

To solve this problem dynamically, I suggest the following:

Creating the KeyPress event

//Validação de Input na textBox
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '.')
        {
            e.KeyChar = ',';
        }

        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ','))
        {
            e.Handled = true;
        }

        if ((e.KeyChar == ',') && ((sender as TextBox).Text.IndexOf(',') > -1))
        {
            e.Handled = true;
        }

        if (Regex.IsMatch((sender as TextBox).Text, @"\,\d\d") && e.KeyChar != 8) //Necessario adicionar "using System.Text.RegularExpressions;"
        {
            e.Handled = true;
        }
    }

And also creating the TextChanged event

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //Caso seja inserido uma "," no inicio coloca "0,"
        if ((sender as TextBox).Text == ",")
        {
            (sender as TextBox).Text = "0,";
            (sender as TextBox).SelectionStart = (sender as TextBox).Text.Length + 1;
        }
    }
    
07.12.2017 / 11:07