简体   繁体   English

如何在VB.NET中为动态控制数组创建事件

[英]How to Create Event for Dynamic Control Array in VB.NET

I'm trying to create an array of checkboxes dynamically and also want to put event to those checkboxes. 我正在尝试动态创建复选框数组,并且还希望将事件放入这些复选框。 How can I do this? 我怎样才能做到这一点?

For example: 例如:

I have a array of checkboxes - Chk1, Chk2. 我有一组复选框-Chk1,Chk2。

I want it to work this way: When I check Chk1, I want to disable Chk2, and when Chk1 is unchecked, Chk2 is enable, and vice versa. 我希望它以这种方式工作:当我检查Chk1时,我想禁用Chk2,而当未选中Chk1时,则启用Chk2,反之亦然。

Your input is greately appreciated. 非常感谢您的投入。

Thanks, 谢谢,

PS: The code is in VB.NET. PS:该代码在VB.NET中。 Thanks. 谢谢。


Thank you all for the inputs. 谢谢大家的投入。 I really appreciated it. 我真的很感激。 Maybe I wasn't very clear on my explanation earlier. 也许我之前的解释不太清楚。

Let's say, I have an array of 6 checkboxes, and I want them to behave in group like this: 假设我有一个由6个复选框组成的数组,并且我希望它们在组中的行为如下:

  • When Chk1 is checked, Chk2 is disabled (grey out), and when we uncheck Chk1, Chk2 is enabled, and Vice Versa. 选中Chk1时,Chk2被禁用(灰色),而当我们取消选中Chk1时,Chk2被启用,反之亦然。

  • When Chk3 is checked, Chk4 is disabled, and when we uncheck Chk3, Chk4 is enabled, and Vice Versa. 选中Chk3时,将禁用Chk4,而取消选中Chk3时,将启用Chk4,反之亦然。

and so on.... 等等....

So this is like each pair of checkboxes in the array perform the CheckChanged event upon each other, but not on any other pair. 因此,这就像数组中的每对复选框都彼此执行CheckChanged事件一样,而不对其他任何一对执行。 So I think OptionButton is not the case in this situation. 所以我认为OptionButton在这种情况下不是这种情况。

Thanks for any suggestions. 感谢您的任何建议。

Assuming that it is ASP.Net, have a look at this "strange" example to see how it works(take your array instead of my static creation): 假设它是ASP.Net,请看下面这个“奇怪”的例子,看看它是如何工作的(用数组代替我的静态创建):

Private Sub WebForm1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    For number As Int32 = 1 To 100
        Dim chk As New CheckBox
        chk.ID = "chk" & number
        chk.Text = chk.ID
        chk.AutoPostBack = True
        AddHandler chk.CheckedChanged, AddressOf onCheckedChanged
        Me.MyChkPanel.Controls.Add(chk)
    Next
End Sub

Private Sub onCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim chk As CheckBox = DirectCast(sender, CheckBox)
    Dim number As Int32 = Int32.Parse(chk.ID.Substring("chk".Length))
    Dim otherChk As CheckBox
    If number Mod 2 = 0 Then
        otherChk = DirectCast(Me.MyChkPanel.FindControl("chk" & (number - 1)), CheckBox)
    Else
        otherChk = DirectCast(Me.MyChkPanel.FindControl("chk" & (number + 1)), CheckBox)
    End If
    otherChk.Enabled = Not chk.Checked
End Sub

Apart from that i can subscribe Hans' suggestion to use RadioButtons or at least a CheckBoxList . 除此之外,我可以接受汉斯的建议使用RadioButtons或至少使用CheckBoxList

An RadioButton would do this automatically and is more conventional. RadioButton会自动执行此操作,并且更加常规。 For an array of checkboxes, you can use a single handler for the entire array: 对于复选框数组,可以对整个数组使用单个处理程序:

Private Sub _CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Integer
If sender.checked Then
  For i = 0 To UBound(chk)
    If chk(i) IsNot sender Then chk(i).Checked = False
  Next i
End If
End Sub

Keep in mind, if you make changes to this handler, that it is called recursively when you set chk(i).checked to false. 请记住,如果对此处理程序进行更改,则在将chk(i).checked设置为false时将以递归方式调用它。 It doesn't matter in this case, because it skips everything when sender.checked is false. 在这种情况下没关系,因为当sender.checked为false时,它会跳过所有内容。

In case you need it, here is one way to set up the array. 如果需要,这是设置阵列的一种方法。 (The index property in the designer disappeared in the upgrade from vb6 to .net, so you have to make an array of controls in the code now.) (从vb6升级到.net时,设计器中的index属性消失了,因此您现在必须在代码中创建一系列控件。)

Public Class Form1
Dim chk(4) As CheckBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, k As Integer
k = 20
For i = 0 To UBound(chk)
  chk(i) = New CheckBox
  Me.Controls.Add(chk(i))
  chk(i).Location = New Point(20, k)
  k = k + chk(0).Height * 1.5 ' or some location
  chk(i).Text = "Checkbox " & i ' some appropriate text
  AddHandler chk(i).CheckedChanged, AddressOf _CheckedChanged
Next i

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM