0

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

Edited by CrashanBurn: n/a

2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by CrashanBurn
0

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

Edited by codeorder: n/a

0

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

Edited by CrashanBurn: n/a

0

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)
0

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

Thanks a lot for helping, working perfect now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.