【问题标题】:Hide Controls on TabControl隐藏 TabControl 上的控件
【发布时间】:2015-07-15 15:33:25
【问题描述】:

在 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

【问题讨论】:

    标签: vb.net tabcontrol


    【解决方案1】:

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

    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”行中有错误,“变量 'ctl' 在封闭块中隐藏了一个变量”
    • 如果你将它粘贴到你的代码块中并留下你的'Dim ctl as Control'语句,那将是一个错误,但我认为这个语句不再是必要的了。所以你可以删除它。我已经编辑了代码以显示一个工作版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2012-11-13
    • 2011-04-27
    • 2010-10-07
    • 2011-03-22
    相关资源
    最近更新 更多