Ver Mensaje Individual
  #7 (permalink)  
Antiguo 08/05/2012, 14:02
Cvilla90
 
Fecha de Ingreso: abril-2010
Mensajes: 65
Antigüedad: 14 años
Puntos: 3
Respuesta: Caja texto solo números y comas VB.NET

Yo investigué muxo sobre ese tema y al final lo conseguí, aquí te paso una clase y los pasos que debes seguir son:

1.- Creas una clase llamada funciones y dentro de esa clase va el sgte codigo:

Public Sub ValPer_Textbox(ByVal vlTipVal As Byte, ByVal Nom_Text As TextBox, ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Select Case vlTipVal
Case 1 'Todos los caracteres
'No hacemos nada

Case 2 'Sólo números
If InStr(1, "0123456789" & Chr(8), e.KeyChar) = 0 Then
e.KeyChar = ""
End If

Case 3 'Sólo letras
If Char.IsLetter(e.KeyChar) Then
e.Handled = False
ElseIf Char.IsControl(e.KeyChar) Then
e.Handled = False
ElseIf Char.IsSeparator(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If

Case 4 'Alfanuméricos
If Char.IsLetter(e.KeyChar) Or Char.IsNumber(e.KeyChar) Then
e.Handled = False
ElseIf Char.IsControl(e.KeyChar) Then
e.Handled = False
ElseIf Char.IsSeparator(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If

Case 5 'Números y punto
If InStr(1, "0123456789." & Chr(8), e.KeyChar) = 0 Then
e.KeyChar = ""
Else
If e.KeyChar = "." And InStr(1, Nom_Text.Text, ".") > 0 Then e.KeyChar = ""
End If

End Select
End Sub


2.- En tu formulario llamas a la clase funciones:

Dim vlFun As New funciones

3.- En el evento keypress del textbox programas la siguiente(el numero 2 lo puedes cambiar por otros valores creada en la clase funciones 1. todos los caracteres, 2. solo numeros, 3.- solo letras.... y asi hasta el numero 5):

Call vlFun.ValPer_Textbox(2, NomtextBox, sender, e)


Espero que te sirva de ayuda...(Y)