• Jueves 28 de Marzo de 2024, 19:57

Autor Tema:  Validar Datos de una caja te Texto C#  (Leído 22052 veces)

prolupe

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Validar Datos de una caja te Texto C#
« en: Lunes 10 de Octubre de 2011, 21:48 »
0
 hola saludos. Solo queiro saber si alguien me puede ayudar a validar un textBox que me acepte solo letras.
Xfa es que me urge validar unas cajas de texto.
Espero su ayuda Gracias.
« última modificación: Lunes 10 de Octubre de 2011, 21:49 por prolupe »

JaviMarciano

  • Miembro activo
  • **
  • Mensajes: 97
    • Ver Perfil
Re:Validar Datos de una caja te Texto C#
« Respuesta #1 en: Lunes 10 de Octubre de 2011, 23:45 »
0
Hola te dejo una clase que uso para validaciones, tiene un método para eso, no permite ingresar números, espero que te sea de ayuda

Código: C#
  1. class cs_Validaciones
  2.    {
  3.    
  4.         public cs_Aux()
  5.         {
  6.  
  7.         }
  8.  
  9.         /// <summary>
  10.         /// Al Presionar Enter en el TextBox salta al próx
  11.         /// </summary>
  12.         /// <param name="sender"></param>
  13.         /// <param name="e"></param>
  14.         public void Funcion(object sender, KeyPressEventArgs e)
  15.         {
  16.             if (e.KeyChar == '\r')
  17.             {
  18.                 e.Handled = true;
  19.  
  20.                 SendKeys.Send("{TAB}");
  21.             }
  22.         }
  23.  
  24.  
  25.  
  26.         /// <summary>
  27.         /// Solo datos numéricos para precios
  28.         /// </summary>
  29.         /// <param name="sender"></param>
  30.         /// <param name="e">e</param>
  31.         /// <param name="tBox1"></param>
  32.         public void Validacion(object sender, KeyPressEventArgs e, Control tBox1)
  33.         {
  34.             if (((e.KeyChar) < 48) && ((e.KeyChar) != 8) || ((e.KeyChar) > 57))
  35.             {
  36.                 e.Handled = true;
  37.             }
  38.             if (e.KeyChar == ',')
  39.                 e.KeyChar = '.';
  40.             //Permitir comas y puntos (si es punto )
  41.             if (e.KeyChar == ',' || e.KeyChar == '.')
  42.                 //si ya hay una coma no permite un nuevo ingreso de esta
  43.                 if (tBox1.Text.Contains(",") || tBox1.Text.Contains("."))
  44.                     e.Handled = true;
  45.                 else
  46.                     e.Handled = false;
  47.         }
  48.  
  49.  
  50.         /// <summary>
  51.         /// No permitir ingresar Letras
  52.         /// </summary>
  53.         /// <param name="parent"></param>
  54.         public void ValidarNumericos(object sender, KeyPressEventArgs e)
  55.         {
  56.             if (((e.KeyChar) < 48) && ((e.KeyChar) != 8) || ((e.KeyChar) > 57))
  57.             {
  58.                 e.Handled = true;
  59.             }
  60.  
  61.         }
  62.  
  63.  
  64.         public void Llenar_Con_Ceros(Control parent)
  65.         {
  66.             TextBox t;
  67.             foreach (Control c in parent.Controls)
  68.             {
  69.                 {
  70.                     t = c as TextBox;
  71.                     if (t != null)
  72.                     {
  73.                         t.Text = "0";
  74.  
  75.                     }
  76.                     if (c.Controls.Count > 0)
  77.                     {
  78.                         Llenar_Con_Ceros(c);
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         public void Limpiar_RichTextBoxes(Control parent)
  85.         {
  86.             RichTextBox t;
  87.             foreach (Control c in parent.Controls)
  88.             {
  89.                 t = c as RichTextBox;
  90.                 if (t != null)
  91.                 {
  92.                     t.Clear();
  93.  
  94.                 }
  95.                 if (c.Controls.Count > 0)
  96.                 {
  97.                     Limpiar_RichTextBoxes(c);
  98.                 }
  99.             }
  100.  
  101.         }
  102.  
  103.         public void limpiarTextBoxes(Control parent)
  104.         {
  105.             TextBox t;
  106.             foreach (Control c in parent.Controls)
  107.             {
  108.                 t = c as TextBox;
  109.                 if (t != null)
  110.                 {
  111.                     t.Clear();
  112.                 }
  113.                 if (c.Controls.Count > 0)
  114.                 {
  115.                     limpiarTextBoxes(c);
  116.                 }
  117.             }
  118.         }
  119.         /// <summary>
  120.         /// Comprueba si los textboxs estás vacíos
  121.         /// </summary>
  122.         /// <param name="parent"></param>
  123.         /// <returns></returns>
  124.         public bool ValidarTextBoxes(Control parent)
  125.         {
  126.             TextBox t;
  127.             foreach (Control c in parent.Controls)
  128.             {
  129.                 t = c as TextBox;
  130.                 if (t != null)
  131.                 {
  132.                     if (t.Text == "")
  133.                         return false;
  134.                 }
  135.                 if (c.Controls.Count > 0)
  136.                 {
  137.                     ValidarTextBoxes(c);
  138.                 }
  139.             }
  140.             return true;
  141.         }
  142.  
  143.  
  144.         public void limpiar_Combos(Control parent)
  145.         {
  146.             ComboBox t;
  147.             foreach (Control c in parent.Controls)
  148.             {
  149.                 t = c as ComboBox;
  150.                 if (t != null)
  151.                 {
  152.                     t.Text = "";
  153.                 }
  154.                 if (c.Controls.Count > 0)
  155.                 {
  156.                     limpiarTextBoxes(c);
  157.                 }
  158.             }
  159.         }
  160.  
  161.  
  162.  
  163.         /// <summary>
  164.         /// No permitir ingresar Numeros
  165.         /// </summary>
  166.         /// <param name="parent"></param>
  167.  
  168.         public void ValidarLetras(object sender, KeyPressEventArgs e)
  169.         {
  170.             if (Char.IsLetter(e.KeyChar))
  171.             {
  172.                 e.Handled = false;
  173.             }
  174.             else if (Char.IsControl(e.KeyChar))
  175.             {
  176.                 e.Handled = false;
  177.             }
  178.             else if (Char.IsSeparator(e.KeyChar))
  179.             {
  180.                 e.Handled = false;
  181.             }
  182.             else
  183.             {
  184.                 e.Handled = true;
  185.             }
  186.        
  187.         }
  188.     }

gerardo alberto

  • Nuevo Miembro
  • *
  • Mensajes: 8
    • Ver Perfil
Re:Validar Datos de una caja te Texto C#
« Respuesta #2 en: Lunes 17 de Octubre de 2011, 20:30 »
0
private void txtxTexto_KeyPress(object sender, KeyPressEventArgs e)
        {

            if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || e.KeyChar == 8 || e.KeyChar == 'ñ' || e.KeyChar == 'Ñ')

                e.Handled = false;
            else
                e.Handled = true;
           
        }
donde 65-90 = A hasta Z, y  donde 97 - 122 = a hasta z.  8 = retroceso y  ñ o Ñ. son los caracteres ascii que podras utilizar.

si los remplazas por 30-39 son los numeros del 0 al 9.