比如果laber1,laber2,laber3....
或者laber1,laber6,laber11..
这两种
------解决方案--------------------
GroupBox1里有多种控件,得加个判断,
For Each lb In Me.GroupBox1.Controls
If TypeOf lb Is Label Then
DirectCast(lb, Label).Text = "abc"
End If
Next
------解决方案--------------------
哦,还有一种
For Each c In Me.GroupBox1.Controls
Dim lbl as Label = TryCast(c, Label)
If (lbl IsNot Nothing) then
lbl.Text = "abc"
End If
Next
------解决方案--------------------
代码具体意思:
基本思路就是遍历Me.GroupBox1.Controls里面的所有的控件
当控件被判定为label的时候去设text属性
上面三种方法的区别就是判断label的方法
第一种是人为的定一个规则
第二种和第三种是去判断控件的类型
------解决方案--------------------
Public Class Class1
Private Label1 As Label
Public Property NewLabel1() As Label
Get
Return Label1
End Get
Set(ByVal value As Label)
Label1 = value
End Set
End Property
Private Label2 As Label
Public Property NewLabel2() As Label
Get
Return Label2
End Get
Set(ByVal value As Label)
Label2 = value
End Set
End Property
Private Label3 As Label
Public Property NewLabel3() As Label
Get
Return Label3
End Get
Set(ByVal value As Label)
Label3 = value
End Set
End Property
Public Sub MySub()
Dim ArrayList1 As New ArrayList
Dim LabelList1 As New Label
ArrayList1.Add(Label1)
ArrayList1.Add(Label2)
ArrayList1.Add(Label3)
For index1 As Integer = 0 To ArrayList1.Count - 1
LabelList1 = ArrayList1.Item(index1)
LabelList1.Text = "您好"
Next
End Sub
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Add1 As New Class1
Add1.NewLabel1 = Me.Label1
Add1.NewLabel2 = Me.Label2
Add1.NewLabel3 = Me.Label3
Add1.MySub()
End Sub
End Class
你想要感谢别忘记及时结贴!