locked
Help me to make this code short RRS feed

  • Question

  • User917353587 posted

    Hi.

    I have 5 text boxes. I make this boxes like values with 2 decimal places with this code:

    Dim aNumber As Double
            If Double.TryParse(TextBox8.Text, aNumber) Then
                TextBox8.Text = String.Format("{0:n2}", aNumber)
            Else
            End If
            If Double.TryParse(TextBox9.Text, aNumber) Then
                TextBox9.Text = String.Format("{0:n2}", aNumber)
            Else
            End If
            If Double.TryParse(TextBox10.Text, aNumber) Then
                TextBox10.Text = String.Format("{0:n2}", aNumber)
            Else
            End If
            If Double.TryParse(TextBox11.Text, aNumber) Then
                TextBox11.Text = String.Format("{0:n2}", aNumber)
            Else
            End If
            If Double.TryParse(TextBox12.Text, aNumber) Then
                TextBox12.Text = String.Format("{0:n2}", aNumber)
            Else
            End If
            If Double.TryParse(TextBox13.Text, aNumber) Then
                TextBox13.Text = String.Format("{0:n2}", aNumber)
            Else
            End If

    Can I make this code short ?

    Friday, October 27, 2017 1:48 PM

Answers

  • User2103319870 posted

    I make this code short ?

    You can move the code to common method and then use that for format the numbers

    Common method

      Public Function FormatNumbers(ByVal inputvalue As String) As String
            Dim aNumber As Double
            Dim result As String
            If Double.TryParse(inputvalue, aNumber) Then
                result = String.Format("{0:n2}", aNumber)
            Else
                result = "Invalid Double Number"
            End If
            Return result
        End Function

    Calling the method

    TextBox8.Text = FormatNumbers(TextBox8.Text)
            TextBox9.Text = FormatNumbers(TextBox9.Text)
            TextBox10.Text = FormatNumbers(TextBox10.Text)
            TextBox11.Text = FormatNumbers(TextBox11.Text)
            TextBox12.Text = FormatNumbers(TextBox12.Text)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 27, 2017 2:54 PM

All replies

  • User2103319870 posted

    I make this code short ?

    You can move the code to common method and then use that for format the numbers

    Common method

      Public Function FormatNumbers(ByVal inputvalue As String) As String
            Dim aNumber As Double
            Dim result As String
            If Double.TryParse(inputvalue, aNumber) Then
                result = String.Format("{0:n2}", aNumber)
            Else
                result = "Invalid Double Number"
            End If
            Return result
        End Function

    Calling the method

    TextBox8.Text = FormatNumbers(TextBox8.Text)
            TextBox9.Text = FormatNumbers(TextBox9.Text)
            TextBox10.Text = FormatNumbers(TextBox10.Text)
            TextBox11.Text = FormatNumbers(TextBox11.Text)
            TextBox12.Text = FormatNumbers(TextBox12.Text)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 27, 2017 2:54 PM
  • User917353587 posted

    Thank you, a2h.

    With first part of this code I can make all I want with all other textboxes. Perfect !

    Regards

    Tuesday, October 31, 2017 11:37 AM
  • User917353587 posted

    All works, but ...

    If the value is 1900.00 /without interval between numbers/ - the result is correct.

    If the value is 1 900.00 /with interval/ - the result is incorrect. The system accept only value 1.

    How make 1 900.00 to be 1900.00 ?

    regards

    Friday, November 3, 2017 10:51 AM
  • User2103319870 posted

    How make 1 900.00 to be 1900.00 ?

    You can remove the whitespace from inputvalue using Replace method. Change the method like given below

     Public Function FormatNumbers(ByVal inputvalue As String) As String
            Dim aNumber As Double
            Dim result As String
            'Remove unwanted spaces from input value
            inputvalue = inputvalue.Replace(" ", "")
            If Double.TryParse(inputvalue, aNumber) Then
                result = String.Format("{0:n2}", aNumber)
            Else
                result = "Invalid Double Number"
            End If
            Return result
        End Function
    Friday, November 3, 2017 2:52 PM
  • User917353587 posted

    Thank you.

    I will try and say the result.

    Regards

    Saturday, November 4, 2017 8:06 AM
  • User917353587 posted

    Hi again.

    This code

    inputvalue = inputvalue.Replace(" ", "")

    not work.

    Please see the result :

    Tuesday, November 7, 2017 6:15 AM