vb.net 禁用运行时创建的按钮
嘿,我在运行时创建了动态按钮,我想在用户单击表单按钮时禁用它们。
这是我对该按钮的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用按钮对象名称的字符串表示形式来引用按钮对象。不要使用
buttonNames
(我假设它是一个字符串数组),而是使用一个按钮数组并将每个按钮添加到其中。然后循环遍历该数组(正如您在此处所做的那样)在每个数组上设置enabled = false。因此,在创建每个图片框之前,请声明一个数组来存储它们:
然后在创建每个图片框时,将它们添加到数组中。创建完它们后,您可以像这样禁用它们:
我创建了一个带有两个按钮 cmdGo 和 cmdDisable 的表单,以更完整地演示这一点:
注意
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:
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Notice how
MyPictureBoxes
is scoped for the whole form so it's accessible to both subs.