我们说我在文本框和标签中有以下内容:
txtAmount1_1
txtAmount1_2
txtAmount1_3
txtAmount2_1
txtAmount2_2
txtAmount2_3
lblTotal1
lblTotal2
lblGrandTotal
我想将txtAmount1_1,txtAmount1_2和txtAmount1_3的总和显示为lblTotal1,将txtAmount2_1,txtAmount2_2和txtAmount2_3的总和显示为lblTotal2,将lblTotal1和lblTotal2的总和显示为lblGrandTotal。所以我输入了以下代码:
Private Sub txtAmount1_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount1_1.TextChanged
Dim a As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount1_2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount1_2.TextChanged
Dim a As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount1_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount1_3.TextChanged
Dim a As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount2_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2_1.TextChanged
Dim a As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount2_2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2_2.TextChanged
Dim a As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount2_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2_3.TextChanged
Dim a As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub lblTotal1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal1.Click
Dim a As Decimal
Dim b As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
b = Val(lblTotal1.Text) + Val(lblTotal2.Text)
lblGrandTotal.Text = Val(b)
End Sub
Private Sub lblTotal2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal2.Click
Dim a As Decimal
Dim b As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal1.Text = Val(a)
b = Val(lblTotal1.Text) + Val(lblTotal2.Text)
lblGrandTotal.Text = Val(b)
End Sub
Private Sub lblGrandTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblGrandTotal.Click
Dim a As Decimal
a = Val(lblTotal1.Text) + Val(lblTotal2.Text)
lblGrandTotal.Text = Val(a)
End Sub
lblTotal1和lblTotal2工作正常(我的意思是它正确显示文本框中值的总和),但lblGrandTotal不起作用(不显示lblTotal1和lblTotal2的总和)。为什么会这样?我的代码有问题吗?然后在哪里?
答案 0 :(得分:1)
我认为你的程序有点凌乱,我没有看到你想要的概念,但我试着修改你的一些程序,或者我认为这就是你想要的......
Private Sub txtAmount1_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount1_1.TextChanged
Dim a As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount1_2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount1_2.TextChanged
Dim a As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount1_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount1_3.TextChanged
Dim a As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
End Sub
Private Sub txtAmount2_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2_1.TextChanged
Dim a As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal2.Text = Val(a)
End Sub
Private Sub txtAmount2_2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2_2.TextChanged
Dim a As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal2.Text = Val(a)
End Sub
Private Sub txtAmount2_3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount2_3.TextChanged
Dim a As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal2.Text = Val(a)
End Sub
Private Sub lblTotal1_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal1.TextChanged
Dim a As Decimal
Dim b As Decimal
a = Val(txtAmount1_1.Text) + Val(txtAmount1_2.Text) + Val(txtAmount1_3.Text)
lblTotal1.Text = Val(a)
b = Val(lblTotal1.Text) + Val(lblTotal2.Text)
lblGrandTotal.Text = Val(b)
End Sub
Private Sub lblTotal2_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal2.TextChanged
Dim a As Decimal
Dim b As Decimal
a = Val(txtAmount2_1.Text) + Val(txtAmount2_2.Text) + Val(txtAmount2_3.Text)
lblTotal1.Text = Val(a)
b = Val(lblTotal1.Text) + Val(lblTotal2.Text)
lblGrandTotal.Text = Val(b)
End Sub
我注意到lblTotal2是常量或不变化。我认为这是lblTotal1的相同概念所以我更改它以添加txtAmount2_1,txtAmount2_2,txtAmount2_3然后我将标签上的click事件更改为Text change,因为事件只会在您单击标签时触发,我知道您想要是自动显示所有文本框的总和,并显示两个lblTotal1和lblTotal2的最终总数......
答案 1 :(得分:0)
我已经发现了我的错误。这是一个印刷错误(而不是Val(txtAmount1_1.Text)
,我只是键入Val(txtAmount1_1)
)。它现在工作正常。我确实使用过Val,因为没有使用它会让我的代码出错。