Advertisement
3_2004-2005 Files/ File Controls/ Input/ Output #149241

How to dynamically create multiple controls?

Automatically add controls to the form at runtime, (by code, programmatically) and react to their events

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Dim chk() As CheckBox
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim i As Integer
  For i = 0 To 5
   ReDim Preserve chk(i)
   chk(i) = New CheckBox()
   chk(i).Text = "check " & i
   chk(i).Top = chk(i).Height * i
   chk(i).Left = 0
   chk(i).Name = "chk" & i
   Me.Controls.Add(chk(i))
   AddHandler chk(i).CheckStateChanged, AddressOf chk_CheckedChanged
  Next
 End Sub
 Private Sub chk_CheckedChanged(ByVal sender As System.Object, ByVal e As 
System.EventArgs)
  MsgBox("checkbox " & sender.name & "'s state changed to " & sender.Checked)
 End Sub
Original Comments (3)
Recovered from Wayback Machine