I have created a currency textbox formatting with the _Validating event. I use this formatting for six textboxes that have to have their values added. So what happens is after I typed in an amount and left the textbox, the amount (the text I inputted) becomes formatted, and the 'Total' (where the sum is displayed) is automatically updated (I did this using the _TextChanged Event).

For example, I have inputted '2000', '3746.5', '890' and '567.34'. After I leave each textbox where I typed in those values, they become '2,000.00', '3,746.50', '890.00', and '567.34'. The 'Total' displays '7,203.84'. I have no problem with this, but I do have a problem when it comes to viewing of records.

Whenever I try to view a previously saved record, the six textboxes get filled up with values (based from what was saved in the record) but they don't automatically sum up ('Total' displays the last inputted text and not the sum of the current values in the six textboxes). I still have to make changes to any one of those six textboxes for the addition to happen (for the 'Total' to update).

I know that maybe this is because of the _Validating event (the event 'validates' the text in the textbox first) but what I want is for the addition to fire up instantly on record-viewing.

What can be done about this?

By the way, here are the codes that I used:

'this is the validating event
Private Sub txtAmount2_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAmount2.Validating
        Dim LenStr As String = Len(txtAmount2.Text)
        Dim RawString As String
        LenStr = LenStr - 4
        If InStr(1, txtAmount2.Text, "PHP") <> 0 Then
            RawString = Microsoft.VisualBasic.Right(txtAmount2.Text, LenStr)
            RawString = RawString.Replace(",", "")
        Else
            If Double.TryParse(txtAmount2.Text, vbNull) Then
                amount2 = txtAmount2.Text
                txtAmount2.Text = FormatNumber(txtAmount2.Text, 2, TriState.False, , TriState.True)
                'TextBox1.Text = "PHP " + TextBox1.Text
                txtAmount2.BackColor = Color.White
            Else
                txtAmount2.BackColor = Color.DarkOrange
                'I removed e.Cancel = True because what if I accidentally 'tab-focused' on a textbox but didn't want to input anything?
                'e.Cancel = True
            End If
        End If
    End Sub

'and this is what I call the 'addition' event
    Private Sub txtAmount2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2.TextChanged
        Dim a As Double
        a = Val(amount1) + Val(amount2) + Val(amount3) + Val(amount4) + Val(amount5)
        totalu = a
        TextBox1.Text = Format(Val(a), "#,##0.00")
    End Sub

Recommended Answers

All 6 Replies

call the addition code after your code that retrieves the record values. At that point the textboxes will have their amounts displayed in them so you can safely call the addition function. That would mean taking it out of the _TextChanged function and having the _TextChanged function calling it as well.

I put the addition code in a separate sub (Private Sub) so that I can call it anytime I need it. Am I doing it right?

Yep, has that solved the problem? If you call that after retrieving and loading your values?

No. Same thing. I still had to 'make changes' to the textboxes so that it will add up. :( Is there any way to format the textbox without using the _Validating event? I tried LostFocus and Leave but it behaves just like Validating (I mean, you know, no sum).

Additional: I am getting my record from a datagridview on a separate form. So when I double-click on a row that I want to view, the values are copied to the fields in my form (the form where the 'problem' textboxes are located). I tried to call the function I made (addition()) from the datagridview doubleclick event but no changes. Still not getting the sum. :(

Can't you just compute the total at the moment you populate the fields based on the values that are passed by the dgv? The values are probably kept as numeric values no, so you could set the total textbox with the total, formatted as you choose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.