我正在尝试一些我不知道是否可以做的事情。
当我按下一个按钮时,许多标签的属性.visible必须改变,我正在这样做:
Private Sub AlakranPositionsButton_Click(sender As Object, e As EventArgs) Handles AlakranPositionsButton.Click
If AlakranLabel.Visible = False Then
Label4.Visible = True
Label5.Visible = True
AlakranLabel.Visible = True
Alakran1Label.Visible = True
Alakran2Label.Visible = True
Alakran3Label.Visible = True
Alakran4Label.Visible = True
Alakran5Label.Visible = True
Alakran6Label.Visible = True
Alakran7Label.Visible = True
Alakran8Label.Visible = True
Alakran9Label.Visible = True
Alakran10Label.Visible = True
Alakran1YTextBox.Visible = True
Alakran2YTextBox.Visible = True
Alakran3YTextBox.Visible = True
Alakran4YTextBox.Visible = True
Alakran5YTextBox.Visible = True
Alakran6YTextBox.Visible = True
Alakran7YTextBox.Visible = True
Alakran8YTextBox.Visible = True
Alakran9YTextBox.Visible = True
Alakran10YTextBox.Visible = True
Alakran1XTextBox.Visible = True
Alakran2XTextBox.Visible = True
Alakran3XTextBox.Visible = True
Alakran4XTextBox.Visible = True
Alakran5XTextBox.Visible = True
Alakran6XTextBox.Visible = True
Alakran7XTextBox.Visible = True
Alakran8XTextBox.Visible = True
Alakran9XTextBox.Visible = True
Alakran10XTextBox.Visible = True
Else
Label4.Visible = False
Label5.Visible = False
AlakranLabel.Visible = False
Alakran1Label.Visible = False
Alakran2Label.Visible = False
Alakran3Label.Visible = False
Alakran4Label.Visible = False
Alakran5Label.Visible = False
Alakran6Label.Visible = False
Alakran7Label.Visible = False
Alakran8Label.Visible = False
Alakran9Label.Visible = False
Alakran10Label.Visible = False
Alakran1YTextBox.Visible = False
Alakran2YTextBox.Visible = False
Alakran3YTextBox.Visible = False
Alakran4YTextBox.Visible = False
Alakran5YTextBox.Visible = False
Alakran6YTextBox.Visible = False
Alakran7YTextBox.Visible = False
Alakran8YTextBox.Visible = False
Alakran9YTextBox.Visible = False
Alakran10YTextBox.Visible = False
Alakran1XTextBox.Visible = False
Alakran2XTextBox.Visible = False
Alakran3XTextBox.Visible = False
Alakran4XTextBox.Visible = False
Alakran5XTextBox.Visible = False
Alakran6XTextBox.Visible = False
Alakran7XTextBox.Visible = False
Alakran8XTextBox.Visible = False
Alakran9XTextBox.Visible = False
Alakran10XTextBox.Visible = False
End If
我认为必须有办法做这样的事情:
If AlakranLabel.Visible = False Then
For i As Integer = 0 To 20
Label(i).visible = False
Next
Else
For i As Integer = 0 To 20
Label(i).visible = True
Next
End If
有人知道怎么做吗?我无法看到如何做到这一点..
谢谢!
解决方案 列表声明:
Dim listObjectiveLabels As List(Of Label) = New List(Of Label)
Dim listObjectiveTextBox As List(Of TextBox) = New List(Of TextBox)
在列表中添加每个标签和文本框:
listAlakranLabels.Add(AlakranLabel)
listAlakranLabels.Add(Alakran1Label)
listAlakranLabels.Add(Alakran2Label)
listAlakranLabels.Add(Alakran3Label)
listAlakranLabels.Add(Alakran4Label)
listAlakranLabels.Add(Alakran5Label)
listAlakranLabels.Add(Alakran6Label)
listAlakranLabels.Add(Alakran7Label)
listAlakranLabels.Add(Alakran8Label)
listAlakranLabels.Add(Alakran9Label)
listAlakranLabels.Add(Alakran10Label)
listAlakranTextBox.Add(Alakran1YTextBox)
listAlakranTextBox.Add(Alakran2YTextBox)
listAlakranTextBox.Add(Alakran3YTextBox)
listAlakranTextBox.Add(Alakran4YTextBox)
listAlakranTextBox.Add(Alakran5YTextBox)
listAlakranTextBox.Add(Alakran6YTextBox)
listAlakranTextBox.Add(Alakran7YTextBox)
listAlakranTextBox.Add(Alakran8YTextBox)
listAlakranTextBox.Add(Alakran9YTextBox)
listAlakranTextBox.Add(Alakran10YTextBox)
listAlakranTextBox.Add(Alakran1XTextBox)
listAlakranTextBox.Add(Alakran2XTextBox)
listAlakranTextBox.Add(Alakran3XTextBox)
listAlakranTextBox.Add(Alakran4XTextBox)
listAlakranTextBox.Add(Alakran5XTextBox)
listAlakranTextBox.Add(Alakran6XTextBox)
listAlakranTextBox.Add(Alakran7XTextBox)
listAlakranTextBox.Add(Alakran8XTextBox)
listAlakranTextBox.Add(Alakran9XTextBox)
listAlakranTextBox.Add(Alakran10XTextBox)
用于每个句子:
For Each l As Label In listAlakranLabels
l.Visible = True
Next
For Each l As TextBox In listAlakranTextBox
l.Visible = True
Next
答案 0 :(得分:2)
您有两个选择:
1.-将标签添加到List<Label>
并使用For Each
设置其Visible
属性:
Dim allLabels As List(Of Label) = New List(Of Label);
allLabels.Add(Label4)
allLabels.Add(Label5)
allLabels.Add(AlakranLabel)
...
For Each l As Label In allLabels
l.Visible = True
Next
2.-如果要显示/隐藏容器内的所有标签,可以For Each
容器Controls
收集,如下所示:
For Each l As Label In Me.Controls.OfType(Of Label)
l.Visible = True
Next
修改强>
您可以使用Find
集合的Controls
方法获得其他选项:
For i As Integer = 1 To 10
Me.Controls.Find("Alakran" & i & "Label", False).FirstOrDefault().Visible = True
Next
这会将所有标签从Alakran1Label
设置为Alakran10Label
,并将其设置为True
答案 1 :(得分:0)
您可以通过以下方式迭代表单(或容器)中Label
或Textbox
的所有实例:
将其可见性设置为false。
Dim lbl_holder as Control
For each lbl_holder in Me.Controls
If typeof lbl_holder is Label or Typeof lbl_holder is Textbox then
lbl_holder.Visible = False
End If
Next