0

This is my code for an array textbox . I want to show the addition of array textbox boxpoamount in txttotalamount. but instead of adding each of the value of boxpoamount. the codes only multiply the last textbox of poamount to the number of how many the boxpoamount.

 Dim newpoamountbox As New List(Of TextBox)


 Private Sub controlall(ByVal controlcount As Integer)

  Dim boxpoamount As TextBox

  For i As Integer = 1 To controlcount
            boxpoamount = New TextBox
            boxpoamount.Size = New Drawing.Size(100, 20)
            boxpoamount.Location = New Drawing.Point(1013, 542 + 58 * (i - 1))
            newpoamountbox.Add(boxpoamount)
            Me.Controls.Add(boxpoamount)
        Next

       Private Sub boxunitpricecom(ByRef boxpoqty As TextBox, ByRef boxpounitprice As TextBox, ByRef boxpoamount As TextBox)
            'MessageBox.Show("right")

            Dim var1 As String
            Dim var2 As String
            Dim var3 As String
            'Dim var4 As String
            'Dim amount As String

            Try

                var1 = Val(boxpoqty.Text)
                var2 = Val(boxpounitprice.Text)
                var3 = var1 * var2
                boxpoamount.Text = var3



                Dim txt As TextBox
                Dim Sum As Integer
                Dim controlall As Integer = Val(txtpoitemno.Text)
                For I = 1 To controlall
                    txt = CType(Me.Controls(boxpoamount.Text + I.ToString()), TextBox)
                    Sum = Sum + Double.Parse(boxpoamount.Text)

                Next I
                txttotalamount.Text = Sum.ToString()

            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                cmd.Dispose()
                conn.Close()

            End Try

        End Sub
5
  • check this line: txt = CType(Me.Controls(boxpoamount.Text + I.ToString()), TextBox) what are you doing with txt? Commented Mar 2, 2016 at 8:31
  • What are the instance names of the textboxes you want to check? Do you have their instances? Commented Mar 2, 2016 at 8:35
  • it should came from boxpoamount and show the total in txttotalamount Commented Mar 2, 2016 at 8:37
  • perhaps, you should do boxpoamount.Name = "mytb" + i to help yourself finding the instance of the textboxes later... ah, I see, they are in the list. Then you are good. :) Commented Mar 2, 2016 at 8:37
  • what? and where should i put the boxpoamount.name Commented Mar 2, 2016 at 8:39

1 Answer 1

1

Change this:

Dim txt As TextBox
Dim Sum As Integer
Dim controlall As Integer = Val(txtpoitemno.Text)
For I = 1 To controlall
    txt = CType(Me.Controls(boxpoamount.Text + I.ToString()), TextBox)
    Sum = Sum + Double.Parse(boxpoamount.Text)
Next I

Into looping for your List

Dim Sum As Integer = 0
Dim Val As Integer = 0
For Each tb As TextBox In newpoamountbox
    If Integer.TryParse(tb.Text, Val) Then
        Sum += Convert.ToInt32(tb.Text)
    End If
Next

Then assign the Sum to the text box showing the result... (use TryParse just to be safe)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.