这是我的数组文本框的代码。我想在txttotalamount中显示添加数组文本框boxpoamount。但不是添加boxpoamount的每个值。代码只将poamount的最后一个文本框与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
答案 0 :(得分:1)
改变这个:
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
进入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
然后将Sum
分配给显示结果的文本框...(使用TryParse
只是为了安全)