Question add format string

mimoleo

New member
Joined
Jun 4, 2013
Messages
3
Programming Experience
Beginner
295920.png
i have a project summing textbox which contains numbers formatted rupiah with code
Txt_bil1.Text = Format(Val(Txt_bil1.Text), "Rp #,#")
Txt_bil2.Text = Format(Val(Txt_bil2.Text), "Rp #,#")
I ask, how do I add txt_bil1.text with txt_bil2.text? Thanks
 
Where is the data coming from in the first place? Is the user entering it or is it being loaded in code? Also, will the system culture always be Indonesian or will it possibly be something else?
 
data first place coming from users type the number in the textbox and press enter then this figure into rupiah. system culture always be Indonesian.
 
In that case, I would suggest something like this:
Imports System.ComponentModel
Imports System.Globalization

Public Class Form1

    Private currencyValueByField As Dictionary(Of TextBox, Decimal?)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Store the currency value against each TextBox.
        currencyValueByField = New Dictionary(Of TextBox, Decimal?) From {{TextBox1, Nothing},
                                                                          {TextBox2, Nothing}}
    End Sub

    Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter, TextBox1.Enter
        'Get the control that has focus and the currency value it contains.
        Dim field = DirectCast(sender, TextBox)
        Dim currency = currencyValueByField(field)

        If currency.HasValue Then
            'Remove currency formatting when a control has focus.
            field.Text = currency.ToString()
        End If
    End Sub

    Private Sub TextBoxes_Validating(sender As Object, e As CancelEventArgs) Handles TextBox2.Validating,
                                                                                     TextBox1.Validating
        'Get the control to validate and the input it contains.
        Dim field = DirectCast(sender, TextBox)
        Dim input = field.Text

        'Only validate if there is data.
        If input <> String.Empty Then
            Dim currency As Decimal

            'Try to convert the input to a number, allowing currency formatting.
            If Not Decimal.TryParse(input, NumberStyles.Currency, Nothing, currency) Then
                'Validation has failed so highlight the invalid input.
                field.SelectAll()
                field.HideSelection = False

                MessageBox.Show("Please enter a valid currency amount.",
                                "Invalid Input",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error)

                field.HideSelection = True

                'Don't let the user leave until they enter a valid value.
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub TextBoxes_Validated(sender As Object, e As EventArgs) Handles TextBox2.Validated,
                                                                              TextBox1.Validated
        'Get the control that passed validation and the input it contains.
        Dim field = DirectCast(sender, TextBox)
        Dim input = field.Text

        If input = String.Empty Then
            'There is no data to format
            currencyValueByField(field) = Nothing
        Else
            'Convert the input to a number, allowing currency formatting.
            Dim currency = Decimal.Parse(input, NumberStyles.Currency, Nothing)

            'Format the data as currency.
            field.Text = currency.ToString("C")

            'Remember the number for future use.
            currencyValueByField(field) = currency
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim currency1 = currencyValueByField(TextBox1)
        Dim currency2 = currencyValueByField(TextBox1)
        Dim total = If(currency1.HasValue, currency1.Value, Decimal.Zero) +
                    If(currency2.HasValue, currency2.Value, Decimal.Zero)

        'Display the combined values as currency.
        MessageBox.Show(total.ToString("C"))
    End Sub

End Class
 
Back
Top