How to execute a method inside a textbox only if the user gives enter?

Asked

Viewed 880 times

1

I am trying to execute a method within a textbox, only if the user gives enter. Check that I have to use Keypress, but I’m not able to apply. I want when the user puts the product code inside the textbox and enter, he can see the product. Code Below :

        private void textBox1_TextChanged(object sender, EventArgs e)
    {


        try
        {
            int Codigo = Convert.ToInt32(txtCodigo.Text);

            ProdutoDAL d = new ProdutoDAL();

            Produto p = d.ConsultarProduto(Codigo);

            if (p != null)
            {
                txtDescricao.Text = Convert.ToString(p.Estoque_descricao);
                txtReferencia.Text = Convert.ToString(p.Estoque_referencia);
                txtComplemento.Text = Convert.ToString(p.Estoque_descricao2);
                txtCodBarras.Text = Convert.ToString(p.Estoque_codbarras);
                txtCustoMedio.Text = Convert.ToString(p.Estoque_customedio);
                txtValorAVista.Text = Convert.ToString(p.Estoque_avista);
                txtValorTabela.Text = Convert.ToString(p.Estoque_tabela);
                txtValorPromocao.Text = Convert.ToString(p.Estoque_promocao);
                txtValorComDesconto.Text = Convert.ToString(p.Estoque_desconto);
                txtLinha.Text = Convert.ToString(p.Estoque_linha);
                txtSetor.Text = Convert.ToString(p.Estoque_setor);
                txtfigura.Text = Convert.ToString(p.Estoque_figura);
                txtMarca.Text = Convert.ToString(p.Estoque_marca);
            }
            else
            {
                MessageBox.Show("PRODUTO NAO CADASTRADO");

            }


        }
        catch (Exception ex)
        {

            throw new Exception("ERRO AO CONSULTAR PRODUTO" + ex.Message);
        }

    }
  • C# is not my thing but knowing that it is used for the web is worth asking: is this textbox HTML? If it is you have to capture the keypress with Javascript (jQuery using something like keyup or change). If it is web warns that I give a help on the JS side. ;)

1 answer

1

If only when the key is used Enter may use the following:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        // Seu codigo aqui
    }
}

Here, the event KeyDown will be triggered as soon as the user loads Enter (the event KeyPress wait for the user to press and release the key).

If on the other hand it is always when the user completes the action of completing the textbox:

private void textBox1_Validated(object sender, System.EventArgs e)
{

}

Here, the event Validated will be triggered when the contents of the textbox is validated.

Normally there is no validation by default, but if you want to add your own logic (see if the product code is correct for example) you can do so using the event Validating (occurs when control loses the phocus but before it gets out of control itself).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.