I am still learing VB and have run into a problem with no decent tutorial. I have created a dynamic form that generates a Textbox and a Update button in each cycle of a loop.
I have declared the following global variables:
Dim tbRef As Textbox
WithEvents btnUpdate As Button
and later in the loop the following
Do Until counter = Maxrows
counter = counter + 1
...
tbRef = New TextBox
...
Me.Controls.Add(tbRef)
btnUpdate = New button
...
AddHandler btnUpdate.Click, AddressOf btnUpdate_Click
Me.Controls.Add(btnUpdate)
...
tbRef.Text = ds.Tables("Records").Rows(counter - 1).Item(0)
Loop
And Finally
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
UpdateForm.tbRef.Text = Me.tbRef.Text
UpdateForm.Show()
End Sub
My Problem is:
The code generates the correct layout and the correct controls, and the button works fine if only one result is returned. If there is more than one button created, all the buttons refer to the contents of the last Textbox generated. The Only answer I got on the internet was that I must somehow use Ctype/DirectCast to cast the contents of each textbox to the button generated with it, but I cant find any tutorial on how to use these Operators in this context. Any help would be greatly appreciated.