0

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

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
stacks
  • 221
  • 3
  • 13
  • I don't use VB.Net (C# dev here) but I guess with the help of keyDown/KeyPress event you can manage to do it, here's a [question](https://stackoverflow.com/questions/2752424/detecting-enter-keypress-on-vb-net) talking about this in VB.Net – Zoma Mar 06 '19 at 13:08
  • .NET doesn't have the VB6 equivalent of control collections. – HardCode Mar 06 '19 at 18:48
  • 1
    I've used the .NET equivalent of the control collection to do some input validations before. [here's more info](https://searchwindevelopment.techtarget.com/answer/How-do-you-use-control-collection-in-VBNET) – Jimmy Smith Mar 07 '19 at 14:10

1 Answers1

0

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
Jimmy Smith
  • 2,452
  • 1
  • 16
  • 19