我在VB6中有以下代码:
Private Sub t_Change(Index As Integer)
Iznos = 0
For i = 1 To 4
Iznos = Iznos + t(i).Text
Next
If CDbl(Iznos) > Label13.Caption Then
t(Index).Text = 0
Iznos = 0
t(Index).SelStart = 0
t(Index).SelLength = 1
For i = 1 To 4
Iznos = Iznos + t(i).Text
Next
End If
t(0).Text = Format(Label13.Caption - CDbl(Iznos), "#,##0.00")
End Sub
此代码适用于我的textboxes
(其中4个命名为:t(0),t(1),t(2),t(3)
)。
我正在尝试通过VB.net实现相同的目的,但是我似乎无法全神贯注。
这是我在VB.Net中的代码:
Public Function izracunaj(ByVal s As Object)
Dim ukupniIznos As Decimal = Decimal.Parse(maticnavalutatbox.Text)
Dim poslaniIznos As Decimal = Decimal.Parse(s.text)
Dim iznos As Decimal
Dim allTextboxesPayment() As TextBox = {gotovinaTbox, karticeTbox, ostaloTbox, ziralnoTbox}
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
If CDbl(iznos) > ukupniIznos Then
s.Text = 0
iznos = 0
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
End If
gotovinaTbox.Text = Format(ukupniIznos - CDbl(iznos), "#,##0.00")
End Function
但是如果我这样调用我的函数:
Private Sub ziralnoTbox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ziralnoTbox.TextChanged
izracunaj(sender)
End Sub
它不起作用,因为我的izracunaj
函数已经具有ziralnoTbox
的输入值。如何使用vb.net实现与vb6相同的功能
答案 0 :(得分:0)
如果我正确看待这个问题,则可以利用TextChanged
事件。
Private Sub TextBox1_TextChanged(sender As Object,
e As EventArgs) Handles gotovinaTbox.TextChanged,
karticeTbox.TextChanged,
ostaloTbox.TextChanged,
ziralnoTbox.TextChanged
Dim ukupniIznos As Decimal = Decimal.Parse(maticnavalutatbox.Text)
Dim poslaniIznos As Decimal = Decimal.Parse(s.text)
Dim iznos As Decimal
Dim allTextboxesPayment() As TextBox = {gotovinaTbox, karticeTbox, ostaloTbox, ziralnoTbox}
'gotovinaTbox, quita esto?
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
If CDbl(iznos) > ukupniIznos Then
s.Text = 0
iznos = 0
For Each kontrola As TextBox In allTextboxesPayment
iznos = iznos + Decimal.Parse(kontrola.Text)
Next
End If
gotovinaTbox.Text = Format(ukupniIznos - CDbl(iznos), "#,##0.00")
End Sub