在TabControl上隐藏控件

时间:2015-07-15 15:33:25

标签: vb.net tabcontrol

在VS2013 VB中工作,我有一个TabControl,在各个选项卡上有Button控件,名为Button1,Button2等。我想在表单加载期间为所有按钮设置visible属性为false,但它是不工作我确信我遗漏了一些简单的东西,这是我的代码:

    Dim ctl As Control

    'Loop thru all controls
    For Each ctl In Me.Controls

        'Test that it is a Button and test for name
        If (TypeOf ctl Is Button And Mid(ctl.Name, 1, 6) = "Button") Then

            'Hide the Button
            ctl.Visible = False

        End If

    Next

1 个答案:

答案 0 :(得分:2)

您需要查看标签页集合和控件集合。 尝试这样的事情:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Try
        hideButtons()
    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred ", ex.Message))
    End Try
End Sub

Private Sub hideButtons()

    For Each tp As TabPage In TabControl1.TabPages
        For Each ctl As Control In tp.Controls
            If (TypeOf ctl Is Button And Mid(ctl.Name, 1, 6) = "Button") Then
                ctl.Visible = False
            End If
        Next
    Next

End Sub