Public Class Form1 Private Function AllEnabled(ByVal b As Boolean) As Boolean For i As Integer = 0 To 2 Dim c As CheckBox = CType(Me.Controls("CheckBox" i.ToString), CheckBox) c.Enabled = b Next End Function Private Sub Button1_Click(sender
Public Class Form1 Private Function AllEnabled(ByVal b As Boolean) As Boolean For i As Integer = 0 To 2 Dim c As CheckBox = CType(Me.Controls("CheckBox" & i.ToString), CheckBox) c.Enabled = b Next End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Call AllEnabled(False) End Sub End Class
在c.Enabled = b的b中突出显示错误
(你调用的对象是空的.)
但当我使用checkbox1.enabled = b而不是c.enabled = b工作正常.
所以我看错了不是用b吧?
&安培;我怎样才能解决这个问题 ?
两个可能的原因.您的for循环创建此控件名称:>“CheckBox0”
>“CheckBox1”
>“CheckBox2”
也许你想要1-3或0-1代替.
也许你想以递归方式找到你的复选框,然后你可以使用Find:
For i As Integer = 0 To 2 Dim ctrl = Me.Controls.Find("CheckBox" & i.ToString, True) If ctrl.Length <> 0 Then ctrl(0).Enabled = b 'Find returns an aray' End If Next
旁注:2013我将不再使用这种VB6风格:
Call AllEnabled(False)
只是
AllEnabled(False)