how can I control some controls that will be created automatically during the execution of the application?
My add control
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewCheckbox As CheckBox
For i As Integer = 2 To 14
NewCheckbox = New CheckBox
NewCheckbox.Size = New Drawing.Size(15, 14)
NewCheckbox.Location = New Point(98, 40)
NewCheckbox.Name = "cbcard" & i
Me.Controls.Add(NewCheckbox)
next
My checkbox control add, I gave an example to create a checkbox control, but it should be valid for any form of control, be it textbox or button.
how can i detect these controls that are not created but will be created during execution?
checkbox name will be cbcard1
, cbcard2
, cbcard3
up to 14.
but I want this to be in the form of a handler, or it should probably work with a timer.
Private Sub Checkbox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Checkbox.Changed
If Checkbox.checked = true
MsgBox("")
End If
End Sub
CodePudding user response:
You can subscribe to the event using the AddHandler
syntax.
For i As Integer = 2 To 14
NewCheckbox = New CheckBox
NewCheckbox.Size = New Drawing.Size(15, 14)
NewCheckbox.Location = New Point(98, 40)
NewCheckbox.Name = "cbcard" & i
Me.Controls.Add(NewCheckbox)
'subscribe to the CheckChanged event (as an example)
AddHandler NewCheckbox.CheckChanged, AddressOf Checkbox_Changed
Next
'all dynamically created checkboxes were subscribed to this event
Private Sub Checkbox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)
'sender is the specific checkbox that was changed out of all subscribed checkboxes
Dim checkBox = DirectCast(sender, CheckBox)
If checkbox.checked = true Then MsgBox(checkBox.Name)
End Sub