vb.net禁用运行时创建的按钮

时间:2011-10-28 03:21:04

标签: vb.net button runtime picturebox

嘿,我已经在运行时创建了动态按钮,我想在用户点击表单按钮时禁用它们。

这是我对该按钮的代码:

Dim intXX As Integer = 0

Do Until intXX = intX
    userAvatar(intXX).Enabled = False
    intXX = intXX + 1
Loop

buttonNames是在运行时创建的所有填充按钮名称的数组。但是,在结尾处尝试.enabled = false不起作用。使用在运行时创建的按钮还有哪些其他方法可以做到这一点?

我如何创建按钮是这样的:

private sub createButton()
Dim personAvatar As New PictureBox

With personAvatar 
        .AutoSize = False                        '>                        ^

        If intX = 7 Then
            thePrviousAvatarImg = 0
        End If

        If intX <= 6 Then
            .Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
        ElseIf intX >= 7 And intX <= 14 Then
            .Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
        Else
            Exit Sub
        End If

        .Name = "cmd" & nameOfPerson
        .Size = New System.Drawing.Size(100, 100)
        .TabStop = False
        .Text = ""
        .BorderStyle = BorderStyle.FixedSingle
        .BackgroundImageLayout = ImageLayout.Center
        .BackColor = Color.LightGray
        .BackgroundImage = Image.FromFile(theAvatarDir)
        .Tag = nameOfPerson
        .BringToFront()
    End With

    AddHandler personAvatar.Click, AddressOf personAvatar_Click
    Me.Controls.Add(personAvatar)
    userAvatar(intX) = personAvatar
    intX = intX + 1
End With
End Sub

感谢您的时间和帮助!

大卫

1 个答案:

答案 0 :(得分:3)

您不能使用其名称的字符串表示来引用按钮对象。而不是使用buttonNames(我假设是一个字符串数组),使用一组按钮并添加每个按钮。然后循环遍历该数组(正如您在此处所做的那样),在每个数组上设置enabled = false。

因此,在创建每个图片框之前,请声明一个数组来存储它们:

Dim MyPictureBoxes() as PictureBox

然后在创建每个时,将它们添加到数组中。完成创建后,可以像这样禁用它们:

For Each MyPictureBox In MyPictureBoxes
    Debug.Print(MyPictureBox.Name)
    MyPictureBox.enabled = False
Next

我创建了一个带有两个按钮cmdGo和cmdDisable的表单,以更完整地展示:

Public Class Form1
    Dim MyPictureBoxes(4) As PictureBox

    Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
        Dim personAvatar As New PictureBox
        Dim intX As Integer = 0

        While intX < 5
            personAvatar = New PictureBox
            With personAvatar
                .AutoSize = False
                .Left = intX * 100
                .Top = 100
                .Name = "cmd" & intX
                .Size = New System.Drawing.Size(100, 100)
                .TabStop = False
                .Text = ""
                .BorderStyle = BorderStyle.FixedSingle
                .BackgroundImageLayout = ImageLayout.Center
                .BackColor = Color.LightGray
                .BringToFront()
            End With

            Me.Controls.Add(personAvatar)
            MyPictureBoxes(intX) = personAvatar
            intX = intX + 1
        End While
    End Sub

    Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
        For Each MyPictureBox In MyPictureBoxes
            Debug.Print(MyPictureBox.Name)
            MyPictureBox.Enabled = False
        Next
    End Sub
End Class

请注意MyPictureBoxes如何为整个表单设定范围,以便两个子设备都可以访问。