Rounding Off Text for Multiple Textboxes - ANSWERED

Here is what I want to do: Have the user enter a numeric value into a textbox. Upon leaving, the text (a number) will be checked for the number of decimal places. The text will be changed to meet the 2 decimal places required (either add a "0" to the end, or round to 2 place)
Since I want multiple text boxes to do the same thing, I was hoping to have them all use the same _Leave function.
This is my code, but it crashes:
Private Sub txtBodyX_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles txtBodyX.Leave, txtBodyY.Leave,
txtDieBodyX.Leave, txtLidFootThickness.Leave, txtLidBodyZ.Leave, txtDieBodyY.Leave, txtCollBallDia.Leave,
txtBallPitch.Leave, txtBallDia.Leave

    Dim senderTextBox As Windows.Forms.TextBox = TryCast(sender, Windows.Forms.TextBox)
    Dim txtString As String = senderTextBox.Text, testChar As String, decimalPlaces As Integer, strToNum As Double
    Dim roundedNum As String, newText As String = ""

    'If the first character is a ".", add a leading 0
    If txtString.Substring(0, 1) = "." Then txtString = "0" + txtString
    For i As Integer = 0 To txtString.Length - 1
        testChar = GetChar(txtString, i)
        If testChar = "." Then decimalPlaces = (txtString.Length - 1) - i
        If decimalPlaces = 1 Then newText = txtString + "0"
        If decimalPlaces > 2 Then
            strToNum = CDbl(txtString) 'convert the string to a number
            roundedNum = Math.Round(strToNum, 2) 'round to 2 places
            newText = roundedNum.ToString
        End If
    Next
    senderTextBox.Text = newText
End Sub

Thanks in advance for any help.

Comments

  • Try looking at Format or FormatNumber.

    e.g.
    Function NumTxt(num As Single) NumTxt = Format(num, "0.000") End Function

    Regards,
    Jason Bourhill
    BricsCAD V20 Ultimate
    CAD Concepts

  • Hi Jason, thank you for your response, but I don't think that is the issue.
    I should have been more clear.
    I did change the For statement to be for i = 1 to txtString.Length as getChar starts the index at 1 (not 0, my bad).
    I believe the problem lies in the sender part. I put a breakpoint in that part of the code at "Dim senderTextBox ...." and it allowed me to put a number in the text box, but crashed as soon as the cursor left the textbox.

  • MGorecki
    edited August 2020

    Ok, I figured it out. (slapping my head)
    I was entering a 2 digit number with no decimal and I had nothing to check for that instance.
    Here is the code that works (just in case someone ever needs it):

    Private Sub Textbox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles txtBodyX.Leave, txtBodyY.Leave,
        txtDieBodyX.Leave, txtLidFootThickness.Leave, txtLidBodyZ.Leave, txtDieBodyY.Leave, txtCollBallDia.Leave,
            txtBallPitch.Leave, txtBallDia.Leave    'these are the text boxes that will need to use the same function
    
        Dim senderTextBox As Windows.Forms.TextBox = TryCast(sender, Windows.Forms.TextBox)
        Dim txtString As String = senderTextBox.Text, testChar As String, decimalPlaces As Integer = 0,
            strToNum As Double, roundedNum As String, newText As String = ""
    
        If txtString <> "" Then
            'If the first character is a ".", add a leading 0
            If txtString.Substring(0, 1) = "." Then txtString = "0" + txtString
            For i As Integer = 1 To txtString.Length
                testChar = GetChar(txtString, i)
                If testChar = "." Then
                    decimalPlaces = txtString.Length - i
                    If decimalPlaces = 1 Then newText = txtString + "0"
                    If decimalPlaces = 2 Then newText = txtString
                    If decimalPlaces > 2 Then
                        strToNum = CDbl(txtString) 'convert the string to a number
                        roundedNum = Math.Round(strToNum, 2) 'round to 2 places
                        newText = roundedNum.ToString
                    End If
                End If
            Next
            If decimalPlaces = 0 Then newText = txtString + ".00"
        End If
        senderTextBox.Text = newText     'replace the text in the text box with the text with the correct number of decimal places
    End Sub