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

发布于 2024-12-12 11:54:26 字数 1468 浏览 5 评论 0原文

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

这是我对该按钮的代码:

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

感谢您的时间和帮助!

大卫

Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.

This is the code i have for that button:

Dim intXX As Integer = 0

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

The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?

How i create the buttons are like this:

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

Thanks for your time and help!

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2024-12-19 11:54:26

您不能使用按钮对象名称的字符串表示形式来引用按钮对象。不要使用 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 的作用域是整个的形式,以便两个潜艇都可以访问。

You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.

So before you create each picture box, declare an array to store them:

Dim MyPictureBoxes() as PictureBox

Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:

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

I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:

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

Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文