Search code examples
vb.netvb6vb6-migration

Do code in VB.net on text input before text being shown


I have this code in 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

This code works for my textboxes (4 of them named: t(0),t(1),t(2),t(3)). I am trying to achieve the same with VB.net but i can't seem to wrap my head around it. This is my code in 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

But if I call my function like this:

Private Sub ziralnoTbox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ziralnoTbox.TextChanged
izracunaj(sender)
End Sub

It does not work because my izracunaj function already has the input value of ziralnoTbox. How can i achieve same functionality with vb.net as in vb6


Solution

  • If I'm looking at this correctly, you could make use of the TextChanged event.

    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