Visual Basic.NET - TextBoxNum

 
Vista:

TextBoxNum

Publicado por Luis Alvarez (1 intervención) el 25/05/2007 15:21:00
Hola estoy programando una clase TextBox que acepte solo Números y de Formato a los números pero tengo problemas con el ratón, se supone debe alinear el numero formateado a la izquierda (GoFocus |12345 |, LostFocus | 12.345|) trate con OnEnter pero el ratón no permite Alinear el Numero a la Izq:

'Visual Basic 2005

Public Class MiTextBoxNum
Inherits System.Windows.Forms.TextBox
Private m_Valor As Double
Private m_Pict As String = ""
Private m_Align As System.Windows.Forms.HorizontalAlignment
Private m_SepMil As Char = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator
Private m_SepDec As Char = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator

Public Sub New()
MyBase.New()
MyBase.Text = "0"
End Sub

Public Overridable Property Valor() As Double
Get
Return m_Valor
End Get
Set(ByVal value As Double)
m_Valor = Val(value)
End Set
End Property

Public Overridable Property Formato() As String
Get
Return m_Pict
End Get
Set(ByVal value As String)
m_Pict = value
End Set
End Property

Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
Dim cKey As Char = e.KeyChar
If (Char.IsDigit(cKey)) Or (cKey = m_SepDec) Or (cKey = Chr(8)) Then
If cKey = m_SepDec Then
If InStr(MyBase.Text, m_SepDec) > 0 Then
e.Handled = True
End If
End If
Else
e.Handled = True
End If
MyBase.OnKeyPress(e)
End Sub

Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = Format(Val(LimpiaGet(value)), m_Pict)
Me.Valor = Val(LimpiaGet(value))
End Set
End Property

Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
MyBase.Text = LimpiaGet(MyBase.Text)
MyBase.OnGotFocus(e)
End Sub

Protected Overrides Sub OnLostFocus(ByVal e As EventArgs)
MyBase.TextAlign = m_Align
MyBase.Text = Format(Val(LimpiaGet(MyBase.Text)), m_Pict)
MyBase.OnLostFocus(e)
End Sub

Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
Me.Valor = Val(LimpiaGet(MyBase.Text))
MyBase.OnTextChanged(e)
End Sub

Private Function LimpiaGet(ByVal cText As String)
Return Replace(Replace(cText, m_SepMil, ""), m_SepDec, ".")
End Function

Private Sub MiTextBoxNum_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
m_Align = MyBase.TextAlign
MyBase.TextAlign = HorizontalAlignment.Left
End Sub
End Class
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder