取消选中在vb.net中动态创建的复选框

时间:2019-03-16 15:52:01

标签: vb.net

我想取消选中在加载事件中动态创建的复选框。我想在单击按钮时取消选中它。 我已经在加载事件上初始化了复选框数组,但是现在我想在另一个块上访问它。所以有人可以帮助我如何访问它而不会出现此错误。它给了我错误->

  

对象引用未设置为对象的实例

Public Class AA
Dim cn As New SqlConnection("--my conn string--")
Dim cmd As SqlCommand
Dim ch(10) As CheckBox
Dim s As String = ""
Private Sub AA_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label2.Text = "100"
    cmd = New SqlCommand("select count(*) from tbl_topp", cn)
    cn.Open()
    Dim n As Integer = cmd.ExecuteScalar
    cn.Close()

    ReDim ch(n)
    Dim i As Integer = 1
    Dim x, y As Integer
    cmd = New SqlCommand("select topping,price from tbl_topp", cn)
    cn.Open()
    x = 10
    y = 10
    Dim dr As SqlDataReader = cmd.ExecuteReader
    While dr.Read
        ch(i) = New CheckBox
        With ch(i)
            .Text = dr.GetString(0)
            .Location = New Point(x, y)
            .Size = New Size(80, 20)
            .Font = New Font("Arial", 11)
            .ForeColor = Color.White
            Panel2.Controls.Add(ch(i))
        End With
        AddHandler ch(i).Click, AddressOf ch1
        'Me.Controls.Add(ch(i))
        x += 80
        'y += 30
        If i Mod 3 = 0 Then
            x = 10
            y += 30
        End If
        i = i + 1
    End While
    cn.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 'i want to uncheck it here. What i tried and is not working is
  For i = 0 To totch
       ch(i).Checked = False
  Next
End Sub
End Class

请有人可以帮助我吗? 预先谢谢你

0 个答案:

没有答案