Vb.net 在TabControl上隐藏控件

Vb.net 在TabControl上隐藏控件,vb.net,tabcontrol,Vb.net,Tabcontrol,在VS2013 VB中工作时,我有一个TabControl,它在各个选项卡上都有按钮控件,名为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

在VS2013 VB中工作时,我有一个TabControl,它在各个选项卡上都有按钮控件,名为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

您需要查看选项卡页面集合和控件集合。 试着这样做:

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

该代码在“For Each ctl”行中有一个错误,“Variable'ctl'在封闭块中隐藏一个变量”如果您将它粘贴到代码块中并保留“Dim ctl as Control”语句,这将是一个错误,但我认为不再需要该语句。所以你可以移除它。我已经编辑了代码以显示工作版本。