La Web del Programador: Comunidad de Programadores
 
    Pregunta:  53174 - COMO ARMAR UNA CLASE PARA CONTROLAR LOS TEXT BOX
Autor:  nicolas pandiani
COMO ARMAR UNA CLASE PARA CONTROLAR QUE LOS TEXT BOX INGRESEN NUMEROS, OTRO INGRESE LETRAS Y OTRO LOS DOS
quisiera que si alguien lo sabe poner completo el ejercicio desde la creacion de la clase hasta completar los text box.

Muchas Gracias

  Respuesta:  Omar Salvatierra Cedeño
Que tal Nicolas....

Con respecto a tu pregunta, me imagino debes saber que los textbox tienen el evento keypress, pues ahi es donde deberás programar la llamada a tu función que no es más que validar si la tecla que presionaste es un número o una letra, por ejemplo te pondre los dos ejemplos:

letras:

Private Sub txtnombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnombre.KeyPress
Dim objvalida As New ClSValidatextbox
If (objvalida.validaletras(e.KeyChar)) = False Then
e.Handled = True
Return
End If
GC.ReRegisterForFinalize(objvalida)
GC.Collect()
End Sub

te describo a continuación la clase, es tan sencilla para que la puedas entender:

Imports System.Data.OleDb
Public Class ClSValidatextbox
Dim objconexion As OleDbConnection
Dim objcomando As OleDbCommand
Dim dtreader As OleDbDataReader
Public Sub New()
objconexion = New OleDbConnection("Provider=sqloledb.1;initial catalog=educacion;data source=.; Integrated security=sspi")
objcomando = New OleDbCommand
dtreader = Nothing
End Sub
Public Function validanumero(ByVal caracter As Char) As Boolean
Dim flag As Boolean = False
If caracter = CType("0", Char) Then
flag = True
End If
If caracter = CType("1", Char) Then
flag = True
End If
If caracter = CType("2", Char) Then
flag = True
End If
If caracter = CType("3", Char) Then
flag = True
End If
If caracter = CType("4", Char) Then
flag = True
End If
If caracter = CType("5", Char) Then
flag = True
End If
If caracter = CType("6", Char) Then
flag = True
End If
If caracter = CType("7", Char) Then
flag = True
End If
If caracter = CType("8", Char) Then
flag = True
End If
If caracter = CType("9", Char) Then
flag = True
End If
If caracter = CType("", Char) Then
flag = True
End If
Return (flag)
End Function

Public Function validaletras(ByRef caracter As Char) As Boolean
Dim flag As Boolean = False
If (caracter = CType("A", Char)) Or (caracter = CType("a", Char)) Or (caracter = CType("á", Char)) Then
flag = True
End If
If (caracter = CType("B", Char)) Or (caracter = CType("b", Char)) Then
flag = True
End If
If (caracter = CType("C", Char)) Or (caracter = CType("c", Char)) Then
flag = True
End If
If (caracter = CType("D", Char)) Or (caracter = CType("d", Char)) Then
flag = True
End If
If (caracter = CType("E", Char)) Or (caracter = CType("e", Char)) Or (caracter = CType("é", Char)) Then
flag = True
End If
If (caracter = CType("F", Char)) Or (caracter = CType("f", Char)) Then
flag = True
End If
If (caracter = CType("G", Char)) Or (caracter = CType("g", Char)) Then
flag = True
End If
If (caracter = CType("H", Char)) Or (caracter = CType("h", Char)) Then
flag = True
End If
If (caracter = CType("I", Char)) Or (caracter = CType("i", Char)) Or (caracter = CType("í", Char)) Then
flag = True
End If
If (caracter = CType("J", Char)) Or (caracter = CType("j", Char)) Then
flag = True
End If
If (caracter = CType("K", Char)) Or (caracter = CType("k", Char)) Then
flag = True
End If
If (caracter = CType("L", Char)) Or (caracter = CType("l", Char)) Then
flag = True
End If
If (caracter = CType("M", Char)) Or (caracter = CType("m", Char)) Then
flag = True
End If
If (caracter = CType("N", Char)) Or (caracter = CType("n", Char)) Then
flag = True
End If
If (caracter = CType("Ñ", Char)) Or (caracter = CType("ñ", Char)) Then
flag = True
End If
If (caracter = CType("O", Char)) Or (caracter = CType("o", Char)) Or (caracter = CType("ó", Char)) Then
flag = True
End If
If (caracter = CType("P", Char)) Or (caracter = CType("p", Char)) Then
flag = True
End If
If (caracter = CType("Q", Char)) Or (caracter = CType("q", Char)) Then
flag = True
End If
If (caracter = CType("R", Char)) Or (caracter = CType("r", Char)) Then
flag = True
End If
If (caracter = CType("S", Char)) Or (caracter = CType("s", Char)) Then
flag = True
End If
If (caracter = CType("T", Char)) Or (caracter = CType("t", Char)) Then
flag = True
End If
If (caracter = CType("U", Char)) Or (caracter = CType("u", Char)) Or (caracter = CType("ú", Char)) Then
flag = True
End If
If (caracter = CType("V", Char)) Or (caracter = CType("v", Char)) Then
flag = True
End If
If (caracter = CType("W", Char)) Or (caracter = CType("w", Char)) Then
flag = True
End If
If (caracter = CType("X", Char)) Or (caracter = CType("x", Char)) Then
flag = True
End If
If (caracter = CType("Y", Char)) Or (caracter = CType("y", Char)) Then
flag = True
End If
If (caracter = CType("Z", Char)) Or (caracter = CType("z", Char)) Then
flag = True
End If
If caracter = CType("", Char) Then
flag = True
End If
If caracter = CType(" ", Char) Then
flag = True
End If
Return (flag)
End Function
End Class

La clase va a contener la validación respectiva de numeros o de letras, si te fijas, cada clase retorna un valor boolean, dependiendo si lo que ingresas es numero llamaras a la funcion validatextbox.numero, si es letras validatextboxletras, tal como te puse en el evento keypress, eso lo haras para cada texbox que desees validar y listo. Si deseas puedes modificarlo a tu conveniencia, pero lo que vez ahi me pareció lo más sencillo posible.

Espero te sirva, cualquier duda me escribes que con gusto te ayudo.