リストボックスの方が適切ですが、必要に応じて、フォームのControls
コレクションを使用して名前でラベルにアクセスできます。
Dim _currentLabel As Integer = 1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If _currentLabel <= 20 Then
Dim lbl As Label = CType(Controls("LabelValue" & _currentLabel.ToString()), Label)
lbl.Text = TextBox1.Text
_currentLabel += 1
End If
End Sub
代わりに、コントロールを使用するListBox
には、次のようなことを行う必要があります。
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox1.Items.Count < 20 Then
ListBox1.Items.Add(TextBox1.Text)
End If
End Sub
そして、特定のアイテムを読むには、次のようにします。
Dim secondItem As String = CStr(ListBox1.Items(1))