IDフィールドがデータベースで自動インクリメントされていると仮定したクリックイベントでは、次のようになります。
dim myTxt as New TextBox
myTxt.Name = Me.Controls.Count.ToString '"random" name
myTxt.Location = New Point(10, 10) 'set the position according to your layout
myTxt.Tag = "For DB" 'something to identify it by
myTxt.Visible = True
Me.Controls.Add(myTxt) 'add it to form's control collection.
Me.Refresh 'If in panel etc. change Me with that control.
次に、2回目のクリックイベントで:
... open database, do checks etc.
For each c As Control in Me.Controls
If typeof c Is TextBox AndAlso c.Tag ="For DB" Then
Dim txt As String = CType(c, TextBox).Text
'insert txt into database
End If
Next
(データグリッドでもこれを行うことができます)。
また、配列を使用して作成を実行したり、インデックスによって作成した各テキストボックスを操作したりできます。
For y As UShort = 0 To indexsize_b - 1
For x As UShort = 0 To indexsize_a - 1
array(x, y) = New TextBox
array(x, y).Size = New Size(60, 20)
array(x, y).Location = New Point(5 + 60 * x, 40 + 20 * y)
array(x, y).Visible = True
Me.Controls.Add(array(x, y))
Next
Next
次のような方法でやり取りします。
string s = array(a, b).text