用controls("Button" & i)如何定位放在不同groupbox里的button?
四个button,一个texbox,我写了如下的程序
Public Class Form1Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 3
If Controls("Button" & i).Text = TextBox1.Text Then
Controls("Button" & i).BackColor = Color.Red
End If
Next
End Sub
End Class这样是可以运行的,
但是!!!当我把他们4个放在一个groupbox里之后,就不行了,该怎么办呢?
而且实际情况是我有4个groupbox,51个button……
[解决办法]
所有VB.net的控件都共同有一个用户可以自定的属性叫Tag,你把所有Button的Tag设为“Button”,所有的Groupbox的Tag设为“Groupbox”,所有的Groupbox放在窗体容器内,不要放入任何Groupbox或Panel容器控件内。
Public?Class?Form1
Private?Sub?Button4_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
for each c as control in me.controls
if c.tag="Groupbox" then
for each ct as control in c.controls
if ct.tag="Button" then
if ct.text=textbox1.text then ct.backcolor=color.red
end if
next
end if
next
??End?Sub
End?Class