0

大家好,我的项目中的一行代码出现空引用运行时错误,但是如果我断点然后单步执行,一切正常。有什么想法吗

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim checkbox_l As String = "CheckBox"
    Dim checkbox_i As string
    For i As Integer = 1 To id Step 1
        checkbox_i = checkbox_l + i.ToString
        Try
            If CType(Panel1.Controls(checkbox_i), CheckBox).Checked = True Then
                My.Settings.name = Panel1.Controls("CheckBox" & i).Text
                Call installer_properties()
                Call start_install()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    Next
End Sub

该代码旨在检查是否已选中动态创建的复选框,然后相应地继续前进,但是我在该行中遇到错误

If CType(Panel1.Controls(checkbox_i), CheckBox).Checked = True Then
4

2 回答 2

1

使用 Debug.Assert 捕捉它

Dim c as Control = Panel1.Controls(checkbox_i)
Debug.Assert(c IsNot Nothing)
Dim cb as CheckBox = TryCast(c, CheckBox)
If cb isNot Nothing Then
   If cb.Checked = True Then
      My.Settings.name = cb.Text
      Call installer_properties()
      Call start_install()
   End If
End If
于 2013-03-31T05:41:09.083 回答
0

如果选中则得到结果 1 和 0 - 如果没有,你可以写下:

textbox1.text = Microsoft.VisualBasic.Right(Panel1.Controls("CheckBox" & i).ToString, 1)

于 2016-05-10T03:18:03.060 回答