heredar textbox

29/04/2005 - 21:17 por William Requejo | Informe spam
Hola,
Alguien puede decirme como hago para crear un textbox el cual solo acepte
numeros y luego heredar a partir de este a otros?

Saludos,

William
 

Leer las respuestas

#1 _ C l a u d i n h o _
29/04/2005 - 21:25 | Informe spam
copiar y pegar

saludos


Public Class clsTextboxNumerico

Inherits System.Windows.Forms.TextBox

Private dblValor As Double

Private intNumeroDecimales As Integer = 0

Public Sub New()

MyBase.New()

Me.MaxLength = 11

End Sub

Public ReadOnly Property Vacio() As Boolean

Get

If MyBase.Text = "" Then

Return True

Else

Return False

End If

End Get

End Property

Public Property NumeroDecimales() As Integer

Get

Return intNumeroDecimales

End Get

Set(ByVal Value As Integer)

intNumeroDecimales = Value

End Set

End Property

Public Overrides Property Text() As String

Get

If IsNumeric(valorNumerico(MyBase.Text)) Then

Return valorNumerico(MyBase.Text)

Else

Return 0

End If

End Get

Set(ByVal Value As String)

MyBase.Text = formatoNumerico(Value)

Me.BackColor = Me.BackColor.White

End Set

End Property

Public Sub Activa()

Me.BackColor = Me.BackColor.FromArgb(255, 255, 192)

End Sub

Private Function valorNumerico(ByVal strTexto As String) As String

Dim txtvalortmp As String = ""

Dim txtvalor As String = ""

If MyBase.Text = "" Then

Return "0"

End If

Dim caracter As Char

txtvalortmp = strTexto



For Each caracter In txtvalortmp

If (caracter >= "0" And caracter <= "9") Or caracter = "," Or caracter = "-" Then

txtvalor = txtvalor & caracter

End If

Next

Return txtvalor

End Function

Private Function formatoNumerico(ByVal strTexto As String) As String

Dim txtvalortmp As String = ""

Dim txtvalor As String = ""

Dim caracter As Char

txtvalortmp = strTexto

For Each caracter In txtvalortmp

If (caracter >= "0" And caracter <= "9") Or caracter = "," Or caracter = "-" Then

txtvalor = txtvalor & caracter

End If

Next

If txtvalor <> "" Then

Try

dblValor = txtvalor

If CDbl(txtvalor) <> 0 Then

Return Format(CDbl(txtvalor), "N" & Me.intNumeroDecimales)

Else

Return ""

End If

Catch ex As Exception

dblValor = 0

Return ""

End Try

Else

dblValor = 0

Return ""

End If



End Function

Private Sub clsTextboxNumerico_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus

Me.Text = formatoNumerico(MyBase.Text)

End Sub

Private Sub clsTextboxNumerico_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

If (Char.IsDigit(e.KeyChar) Or e.KeyChar = "," Or e.KeyChar = "." Or e.KeyChar = "" Or e.KeyChar = "-") Then

If (Me.Text.Length < 9) Then

e.Handled = False

Else

If e.KeyChar <> "" Then

e.Handled = False

End If

End If

Else

e.Handled = True

End If

End Sub

Private Sub clsTextboxNumerico_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus

Me.BackColor = Me.BackColor.White

End Sub

End Class

Preguntas similares