Events in controls added during runtime
Hello
In the following code, when the Checkbox is checked, 10 pictureboxes are added to the form.
Code:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
Dim btnName As String
2:
Dim x As Short = 0
3:
4:
For i As Short = 1 To 10
5:
btnName = "PictureBox" & CStr(i)
6:
x += 20
7:
Dim PictureBox As New PictureBox
8:
9:
10:
PictureBox.Name = btnName
11:
Me.Controls.Add(PictureBox)
12:
PictureBox.Location = New Point(x, 10)
13:
PictureBox.Text = "Hello"
PictureBox.BorderStyle = BorderStyle.Fixed3D
PictureBox.Height = 10
PictureBox.Width = 10
14:
15:
16:
17:
Next
End Sub
How do I do a Click event in these pictureboxes?
Re: Events in controls added during runtime
The method that handles an event looks the same regardless of whether the control is added at design time or run time. The only difference is that it won't have a Handles clause if you're using it for run time-added controls. Look at your CheckedChanged event handler to see the Handles clause. For a control added at run time, use an AddHandler statement to register the method as a handler for an event.