Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

EveryTime key is pressed will automatic decimal places in textbox VB.NET

I used keypress event in my textbox. When I pressed "2" and my expected value should be 0.02. However it show "20.02"

What I tried is I set textbox property "RighttoLeft" to yes to start it from right when typing. But I want the keychar I type is not appended to the output of FactorDecimal I used in code.

Please see code below. I based it from @Visual Vincent comment from other post:

 Private Sub txtChequeAmt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtChequeAmt.KeyPress
    If Char.IsDigit(e.KeyChar) = False Then
        e.Handled = True
    End If

    FactorDecimal1.AppendNumber(e.KeyChar)
    txtChequeAmt.Text = FactorDecimal1.ToString()

End Sub

    Public Class FactorDecimal
    Private value As String = "0"

    Public DecimalPlaces As Integer

    Public Sub AppendNumber(Character As Char)
        If Char.IsNumber(Character) = False Then
            Throw New ArgumentException("Input must be a valid numerical Character!", "Character")
            value = (value & Character).TrimStart("0"c)
        Else
            value = (value & Character).TrimStart("0"c)
        End If
    End Sub

    Public Sub RemoveRange(Index As Integer, Length As Integer)
        If value.Length >= Me.DecimalPlaces + 1 AndAlso Index + Length > value.Length - Me.DecimalPlaces Then
            Length -= 1 'Exclude decimal point
        End If

        If Index + Length >= value.Length Then
            Length = value.Length - Index 'Out of range
            value = value.Remove(Index, Length)
        End If

        If value.Length = 0 Then
            value = "0"
        End If
    End Sub

    Public Overrides Function ToString() As String
        Dim Result As Decimal

        If (Decimal.TryParse(value, Result)) = True Then
            Return (Result / (10 ^ Me.DecimalPlaces)).ToString("0." & New String("0"c, Me.DecimalPlaces))

        End If

        Return (Result / (10 ^ Me.DecimalPlaces)).ToString("0." & New String("0"c, Me.DecimalPlaces))
    End Function

    Public Sub New(DecimalPlaces As Integer)
        If DecimalPlaces <= 0 Then
            DecimalPlaces = 1
        End If
        Me.DecimalPlaces = DecimalPlaces
    End Sub
End Class
question from:https://stackoverflow.com/questions/65899162/everytime-key-is-pressed-will-automatic-decimal-places-in-textbox-vb-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to set e.Handled = True if you want to use your calculation into the textbox.

Private Sub txtChequeAmt_KeyPress(sender As Object, e As KeyPressEventArgs) 
    If Char.IsDigit(e.KeyChar) = False Then
        e.Handled = True
        ' No point to continue if it is not a digit because 
        ' in that case you throw into the AppendNumber. Better avoid the exception
        Return
    End If

    FactorDecimal1.AppendNumber(e.KeyChar)
    txtChequeAmt.Text = FactorDecimal1.ToString()

    ' Don't let the forms engine add the number to your textbox 
    ' you have already defined its content 
    e.Handled = True
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...