拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 检测在VB.Net中更改的多个控制元件

检测在VB.Net中更改的多个控制元件

白鹭 - 2022-02-10 2140 0 0

如何控制在应用程序执行期间自动创建的一些控制元件?

我的添加控制元件

  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

我的复选框控制元件添加,我举了一个例子来创建一个复选框控制元件,但它应该对任何形式的控制元件都有效,无论是文本框还是按钮。

如何检测这些未创建但将在执行期间创建的控制元件?复选框名称将为cbcard1, cbcard2,cbcard3最多 14 个。

但我希望它采用处理程序的形式,或者它可能应该与计时器一起使用。

  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

uj5u.com热心网友回复:

您可以使用AddHandler语法订阅事件

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
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *