Bienvenidos

Espero que este Blog llene todas las expectativas que estan buscando

viernes, 16 de noviembre de 2012

EJEMPLOS VALIDAR SOLO NUMEROS EN TEXTBOX Y EVENTO KEYPRESS

Posted by Elvin German 9:06, under | No comments

//SOLO NUMEROS
//-----------------------

//EJEMPLO #1


private void Btn_Procesar_Click(object sender, EventArgs e){

    try    {
        int valor;
        valor =
Convert.ToInt16(textBox1.Text);     }

     catch (Exception)    {

        MessageBox.Show("Digitar solo numeros no letras");     }
}

//EVENTO PARA CUANDO PRESIONEMOS ENTER
//-------------------------------------------------------------------

//EJEMPLO #2


private void textBox1_KeyPress(object sender, KeyPressEventArgs e){

try{if (e.KeyChar == '\r'){
e.Handled =
true;
Btn_Procesar.Focus();
}
}

catch (Exception ex){

MessageBox.Show("Ocurrio un error" + ex.Message);}
}



Para esto es muy importante aprender a trabajar con los eventos, los evento son las Acciones que se Ejecutan Cuando!!

El Evento que vamos a manejar es el Keypress, que se activara cada vez que presione una tecla.
Este código detecta cuando las teclas son presionadas los caracteres y solo dejara escribir los números en un TextBox

//EVENTO QUE SOLO PERMITE NUMEROS
//-------------------------------------------------------
//EJEMPLO #3

private void textBox1_KeyPress(object sender, KeyPressEventArgs e){

//COPIAR DESDE AQUI, Y PONERLO DENTRO DEL CODIGO DEL EVENTO KEYPRESS

  if (Char.IsDigit(e.KeyChar))
{
e.Handled =
false;}

else if (Char.IsControl(e.KeyChar)){
e.Handled =
false;}

else if (Char.IsSeparator(e.KeyChar)){
e.Handled =
false;}

else{
e.Handled =
true;}

//FIN DE SELECCION A COPIAR
}

0 comentarios:

Publicar un comentario