在vb.net 2005中隐藏/显示标签的最佳方法是什么?

时间:2011-08-23 14:07:29

标签: vb.net visual-studio

我的项目中有这个标签

num1.Hide()
num2.Hide()
num3.Hide()
.
.
.
num20.Hide()

隐藏/展示它们的最佳方式是什么?我正在考虑循环,但我无法使其发挥作用。

2 个答案:

答案 0 :(得分:1)

您应该将所有标签放在一个列表中,然后隐藏或显示您的元素,只需在函数中迭代列表即可对所有元素执行操作。

答案 1 :(得分:1)

一个解决方案:

在运行时创建一个标签数组,然后您可以遍历它们以使它们不可见:

'define the array
Dim labelArray(5) As Label

Private Sub createLabels

    'add them to the form
    For i As Integer = 0 To labelArray.GetUpperBound(0)
        initLabel(i, New Point(i * 30, i * 30), i.ToString)
    Next

    'now hide them
    For i As Integer = 0 To labelArray.GetUpperBound(0)
        labelArray(i).Visible = False
    Next

End Sub

Private Sub initLabel(ByVal index As Integer, location As System.Drawing.Point, caption As String)
    labelArray(index) = New Label
    With labelArray(index)
        'set some default properties
        .Name = "LabelArray" + index.ToString
        .Width = 300
        .Height = 100
        .Location = location
        .Text = caption
    End With
    Me.Controls.Add(labelArray(index))
End Sub