Primero crearemos una clase llamada ValidarNumeros
using System;
using System.Text;
using System.Windows.Forms;
namespace Utilidades
{
public class ValidarNumeros
{
public static void validarDecimales(String textBox1, ref KeyPressEventArgs e)
{
if (textBox1.Contains(System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator))
{
if(!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
if (e.KeyChar == '\b')
{
e.Handled = false;
}
}
else
{
if(!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
if(e.KeyChar=='.' || e.KeyChar=='\b')
{
e.Handled = false;
}
}
}
}
}
Ahora simplemente queda usar la clase creada.
Usando la clase.
private void txtLongitudBarreno_KeyPress(object sender, KeyPressEventArgs e)
{
ValidarNumeros.validarDecimales(txtLongitudBarreno.Text,ref e);
}


No hay comentarios:
Publicar un comentario
Nota: solo los miembros de este blog pueden publicar comentarios.