Calculation not working on controls created dynamically

  • Thread starter Thread starter DoruBogyi
  • Start date Start date
D

DoruBogyi

Guest
I am creating payment calculator on some kind of winning on game for the staff to enter payment and app returns refund after tax. Staff can enter as many as payment they want. But my calculation is not working on dynamically created controls. Please guide me. Thank you so much.

Here is how it works. Customer plays $100 and wins it, and tax is 5%. The app should return $95. If the customer gives change of $5, then the refund should be $100.



The calculation button is working perfectly with one entry. But if there are more than one entry, then it returns weird result.

Imports System.ComponentModel

Public Class Calculator



Dim listOfDelete As List(Of Button)
Dim listOfPayment As List(Of TextBox)
Dim listOfTax As List(Of TextBox)
Dim listOfRefund As List(Of Label)
Dim listOfCal As List(Of Button)

Dim btnAdd As Button
Dim flpParent As FlowLayoutPanel

Dim btnDel As Button
Dim txtPayment As TextBox
Dim txtTax As TextBox
Dim lblRefund As Label
Dim btnCal As Button

Dim sTextbox As New TextBox
Dim total As Integer
Sub New()
InitializeComponent()
listOfDelete = New List(Of Button)
listOfPayment = New List(Of TextBox)
listOfTax = New List(Of TextBox)
listOfRefund = New List(Of Label)
listOfCal = New List(Of Button)

btnAdd = New Button
btnAdd.Name = "btnAdd"
btnAdd.Size = New System.Drawing.Size(50, 20)
btnAdd.Location = New System.Drawing.Point(200, 0)
btnAdd.Text = "ADD"
Me.Controls.Add(btnAdd)
AddHandler btnAdd.Click, AddressOf btnTrackInput_Click

'format FlowLayoutPanel
flpParent = New FlowLayoutPanel With {
.Name = "flpHold",
.AutoScroll = True,
.FlowDirection = FlowDirection.LeftToRight,
.Size = New Size(250, 222),
.Location = New System.Drawing.Point(0, 25),
.BackColor = Color.DarkGray,
.BorderStyle = BorderStyle.Fixed3D
}
Me.Controls.Add(flpParent)

End Sub

Private Sub btnTrackInput_Click(sender As Object, e As EventArgs)
Me.SuspendLayout()
btnDel = New Button
btnDel.Width = 25
btnDel.Height = 25
btnDel.Text = "Del"
btnDel.BackColor = Color.Red
AddHandler btnDel.Click, AddressOf btnDelete
listOfDelete.Add(btnDel)

txtPayment = New TextBox
txtPayment.Width = 50
txtPayment.Height = 25
txtPayment.Name = "txtPayment"
txtPayment.Text = "100" 'default value for test
txtPayment.Font = New Font("", 9)
listOfPayment.Add(txtPayment)

txtTax = New TextBox
txtTax.Width = 50
txtTax.Height = 25
txtTax.Name = "txtTax"
txtTax.Font = New Font("", 9)
AddHandler txtTax.Click, AddressOf tb_Click
listOfTax.Add(txtTax)

lblRefund = New Label
lblRefund.Width = 50
lblRefund.Height = 25
lblRefund.Name = "lblRefund"
lblRefund.TextAlign = ContentAlignment.MiddleLeft
lblRefund.Font = New Font("", 9)
lblRefund.Text = calculatePayout(Val(txtPayment.Text), Val(txtTax.Text))
listOfRefund.Add(lblRefund)

btnCal = New Button
btnCal.Width = 40
btnCal.Height = 25
btnCal.Text = "Cal"
btnCal.BackColor = Color.LightGreen
AddHandler btnCal.Click, AddressOf btnCalculation_Click
listOfCal.Add(btnCal)

'add all five controls into FlowLayoutPanel
flpParent.Controls.Add(btnDel)
flpParent.Controls.Add(txtPayment)
flpParent.Controls.Add(txtTax)
flpParent.Controls.Add(lblRefund)
flpParent.Controls.Add(btnCal)


'add FlowLayoutPanel into main Form
Me.Controls.Add(flpParent)
total += Val(txtPayment.Text)
lbltotal.Text = total.ToString()


End Sub

'to delete the five controls row by row
Private Sub btnDelete(sender As Object, e As EventArgs)
Dim senderButton As Button = DirectCast(sender, Button)
Dim payment As TextBox = listOfPayment(listOfDelete.IndexOf(senderButton))
Dim tax As TextBox = listOfTax(listOfDelete.IndexOf(senderButton))
Dim refund As Label = listOfRefund(listOfDelete.IndexOf(senderButton))
Dim cal As Button = listOfCal(listOfDelete.IndexOf(senderButton))

'remove all the controls from flowlayoutpanel on del click
flpParent.Controls.Remove(senderButton)
flpParent.Controls.Remove(payment)
flpParent.Controls.Remove(tax)
flpParent.Controls.Remove(refund)
flpParent.Controls.Remove(cal)
End Sub

Private Sub btnCalculation_Click(sender As Object, e As EventArgs)
Dim senderbutton As Button = DirectCast(sender, Button)
Dim lblPay As Label = listOfRefund(listOfCal.IndexOf(senderbutton))
lblPay.Text = calculatePayout(Val(txtPayment.Text), Val(txtTax.Text)).ToString
End Sub


Private Function calculatePayout(amount As Integer, coin As Integer) As Integer
Dim refund As Integer
refund = ((amount - (amount * 0.05)) + coin)
Return refund
End Function



Public Sub tb_Click(sender As Object, e As EventArgs)
sTextbox.Text = String.Empty
End Sub



End Class

Continue reading...
 
Back
Top