|
Un textBox que
acepte solo Letras en VB .NET
Fecha: 20/Mayo/2003 (Publicado 29/Jun/03) Autor: Angel Enrique Ruiz Pastor (Vzla), [email protected] |
. |
Es te c�digo nos muestra como validar un texbox que acepte solo letras.
Abra un nuevo proyecto, En Proyectos de Visual Basic seleccione la plantilla Aplicaci�n para Windows.
En la Barra de Herramientas Seleccione Proyecto se le desplegara una lista seleccione Agregar M�dulo.
Agregue un Textbox al Formulario y no le cambie el Nombre, D�jelo como textbox1
En el Modulo copie y pegue este c�digo:'****************************************************************************************'* C�digo realizado por Angel Ruiz � (Venezolano) *'****************************************************************************************Function SoloLETRAS(ByVal KeyAscii As Integer) As IntegerKeyAscii = Asc(UCase(Chr(KeyAscii))) 'Transformar letras minusculas a May�sculas' Intercepta un c�digo ASCII recibido admitiendo solamente' letras, adem�s:' deja pasar sin afectar si recibe tecla de Backspace o enterIf InStr("ABCDEFGHIJKLMN�OPQRSTUVWXYZ", Chr(KeyAscii)) = 0 ThenSoloLETRAS = 0ElseSoloLETRAS = KeyAsciiEnd If' teclas adicionales permitidasIf KeyAscii = 8 Then SoloLETRAS = KeyAscii ' BackspaceIf KeyAscii = 13 Then SoloLETRAS = KeyAscii ' EnterEnd Function------------------------------------------------------------------------------------------------------- Ahora valla al Formulario y haga doble click sobre el:Despu�s de la l�nea:Inherits System.Windows.Forms.FormCopie y pegue esta Declaraci�n de VariablePublic KeyAscii As ShortAhora en el evento KeyPress del textBox1 copie y pegue este c�digoDim KeyAscii As Short = CShort(Asc(e.KeyChar))KeyAscii = CShort(SoloLETRAS(KeyAscii))If KeyAscii = 0 Thene.Handled = TrueEnd IfQuedar�a de la siguiente forma:Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPressDim KeyAscii As Short = CShort(Asc(e.KeyChar))KeyAscii = CShort(SoloLETRAS(KeyAscii))If KeyAscii = 0 Thene.Handled = TrueEnd IfEnd SubAhora ejecute su aplicaci�n presionando F5.
Fichero con el c�digo de ejemplo, (ar_SoloLetras.zip - 21,2 KB)