Setting cursor at the position of the string inside the textbox

-1

I'm trying to make a mask for a textbox manually, and I'm trying to imitate the Windows calculator, where when the user types 1000 the code automatically places 1.000 and when I give txtPreco.Text = the course is in the beginning of the string |1.000 . I want to put the cursor at the end of the string 1.000| but I do not know which method or event does this.

    
asked by anonymous 12.11.2018 / 20:13

1 answer

0

To keep the cursor at the end of the content you should use the SelectionStart and SelectionLength properties of TextBox .

See the example below:

private void txtPreco_KeyUp(object sender, KeyEventArgs e)
{
    if (!string.IsNullOrWhiteSpace(txtPreco.Text))
    {
        var valor = Int64.Parse(txtPreco.Text,                                
                           System.Globalization.NumberStyles.AllowThousands);


        txtPreco.Text = string.Format(new System.Globalization.CultureInfo("pt-BR")
                                      "{0:N0}", valor);

        txtPreco.SelectionStart = txtPreco.Text.Length;
        txtPreco.SelectionLength = 0;
    }

}

    
12.11.2018 / 22:55