Hi,

I am trying to get my little program to do some calculations.
So far i got 15 textboxes, named TxtPP(followed by product type),so i got TxtPPproduct1, TxtPPproduct2 etc....
At the bottom of the form i got a disabled textbox which shows the total of all the above textboxes.

Now i don't want to use a button to click when doing the calculations.
I want this to be done every time a value is added to one of the textboxes(so on lost focus).

is there a clean way to do this?

Thanks

Recommended Answers

All 4 Replies

See if this helps.

Public Class Form1
    Private iTotal As Integer '// used to add up the Total of all Product TextBoxes.

    '// renamed from: TxtPPproduct1_TextChanged
    Private Sub myCoolTextBoxes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                Handles TxtPPproduct1.TextChanged, TxtPPproduct2.TextChanged, TxtPPproduct3.TextChanged '// add remaining TextBoxes here.
        iTotal = 0 '// reset.
        For Each ctl As Control In Me.Controls '// loop thru all controls on Form.
            If TypeOf ctl Is TextBox And ctl.Name.StartsWith("TxtPPproduct") Then '// locate TextBoxes that .Name.StartsWith...
                If Not ctl.Text = "" Then iTotal += CInt(ctl.Text) '// if not a empty value in TextBox, add to Total.
            End If
        Next
        txtTOTAL.Text = CStr(iTotal) '// Display total in your TOTAL TextBox.
    End Sub
End Class

Thanks that works like a charm.
One small problem though, if i add up for example 1.1 and 1 it just stays as 2 instead of 2.1

Change Private iTotal As Integer to:

Private iTotal As Double

Change If Not ctl.Text = "" Then iTotal += CInt(ctl.Text) to:

If Not ctl.Text = "" Then iTotal += CDbl(ctl.Text)

Ah i was trying with decimal, stupid mistake on my side.

Thanks a lot for helping, working perfect now.

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.