title: In A Dynamic Form, How do I link the contents of a textbox (tbRef) to the button generated along with it?
body: <p>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.</p>
  
  <p>I have declared the following global variables:</p>
  
  <pre><code>Dim tbRef As Textbox
  WithEvents btnUpdate As Button
  </code></pre>
  
  <p>and later in the loop the following</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And Finally</p>
  
  <pre><code>Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
  UpdateForm.tbRef.Text = Me.tbRef.Text
  UpdateForm.Show()
  End Sub
  </code></pre>
  
  <p>My Problem is:</p>
  
  <p>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.</p>