我编写了下面的代码来删除另一个VB.NET表单中的控件;工作得很好;但是代码无法检测到Form没有NO控件;代码有什么问题:
Sub DeleteControls() ' WORKING
For i As Integer = Form2.Controls.Count - 1 To 0 Step -1
Dim ctrl = Form2.Controls(i)
ctrl.Dispose()
Next
End Sub
Sub TestForm() ' NOT WORKING
If Form2.Controls Is Nothing Then
MessageBox.Show("Form2 has No Controls")
End If
End Sub
由于
答案 0 :(得分:1)
首先,您需要计算表格2上的控件。
"
Dim GetControls As Integer = Form2.Controls.Count"
然后检查GetControls是否小于1"没有控件"
Sub TestForm()
Dim GetControls As Integer = Form2.Controls.Count
If GetControls < 1 Then
MessageBox.Show("Form2 has No Controls")
End If
End Sub