如何在TabControl中隐藏和显示标签页 [英] How can Hide and Show Tab Pages in TabControl

查看:336
本文介绍了如何在TabControl中隐藏和显示标签页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,



我想隐藏标签页3号。我使用的是下面的代码,但没有用....



Sir,

I want to hide tab page no 3. I am using below code but not works....

Dim cmd As System.Data.SqlClient.SqlCommand
        Dim dr As System.Data.SqlClient.SqlDataReader
        Dim cn As System.Data.SqlClient.SqlConnection
        Try
            cn = New System.Data.SqlClient.SqlConnection(DBset())
            cn.Open()
            cmd = New System.Data.SqlClient.SqlCommand("Select [BackupPath] from Owner", cn)
            dr = cmd.ExecuteReader
            While dr.Read
                bkp_path = dr(0)
            End While
        Catch ex As Exception
        End Try
        dr.Close()
        cn.Close()
        If bkp_path = "No" Then
            TabPage3.Hide()
        Else
            TabPage3.Show()
        End If





请帮助我............



Please help me............

推荐答案

这看起来像是一个直接的Visible =真或假的情况。我经常使用TabControls,但我不得不说我从未遇到过需要隐藏TabPage的情况(让它不可见)。

属性可见对此控件没有意义(这个直接来自MSDN)。虽然在DEBUG模式下,您可以在对象树中看到此属性(但无论如何都设置为FALSE)。属性Hide仅隐藏指定TabPage上的控件,而不是TabPage本身。如果您隐藏TabPage,控件将被隐藏,有趣的是,如果您从该页面移动然后返回到调用.Hide()的TabPage,则控件将可见。

我相信促进可见/不可见的唯一方法是,当需要使TabPage不可见时,从TabPages集合中删除TabPage,然后在TabPage可见时将其重新添加。

以下链接有一个解决方案可以为您提供一个良好的起点 - 您需要使用一些额外的代码来管理删除/添加活动。



https://social.msdn.microsoft.com/Forums/windows/en-US/89053adf-7875-4db9-b328-460a7b1f97c0/how-to-remove-hide-tabpage-from-a-tabcontrol- using-vb-net-c-net



以下是一个建议,在两个或多个TabControl之间移动TabPages以保持原始收集是没有必要的。以下解决方案2中给出的设计注意事项可以解决您遇到的一些问题。为了解决您在没有质疑目标的情况下提出的具体问题,我会做类似于以下代码的事情。请注意,这具有删除存储和添加的机制。 DOUBLECLICK方法用于演示目的。您可以将其添加到项目中,重建,将控件添加到表单并进行探索。



This would seem like a straight forward Visible = True or False situation. I use TabControls regularly, but I have to say I have never come across a situation where I needed to hide a TabPage(make it not visible).
The Property Visible is not meaningful for this control (this is straight from MSDN). Although in DEBUG mode you can see this property in the object tree (but it is set to FALSE no matter what). The property Hide only hides the controls on the specified TabPage, not the TabPage itself. If you Hide the TabPage the controls will be hidden, interestingly, if you move from that page and then move back to the TabPage that invoked .Hide(), the controls will then be visible.
I believe the only way to facilitate "visible/non-visible" is by removing the TabPage from the TabPages collection when you need to make it not visible and then adding it back in when the TabPage is to be visible.
The link below has a solution that should give you a good starting point – you will need to manage the remove/add activities with some additional code.

https://social.msdn.microsoft.com/Forums/windows/en-US/89053adf-7875-4db9-b328-460a7b1f97c0/how-to-remove-hide-tabpage-from-a-tabcontrol-using-vb-net-c-net

The following is a suggestion, moving TabPages between two or more TabControls to maintain the original collect isn't necessary. And the design considerations given below in Solution 2 do address some of the problems you face. To solve the specific problem you state without questioning the objective I would do something similar to the following code. Note this has the mechanism to remove-store and add-back. The DOUBLECLICK method is used for demo purposes. You can add this to your project, rebuilt, add the control to a form and explore.

Public Class MyTabControl
    Inherits TabControl

#Region "DELEGATES_EVENTS"
    Public Event HasError(ByVal sender As Object, ByVal e As EventArgs)
#End Region

#Region "VARS"
    Dim RemoveTabs As List(Of TabPage)
    Dim _lastRemoveException As Exception
#End Region

#Region "CONSTRUCTORS_DESTRUCTORS"
    Public Sub New()
        RemoveTabs = New List(Of TabPage)
    End Sub
#End Region

#Region "SUBS_FUNCS"
    Private Sub OnError(ByVal e As Exception)
        _lastRemoveException = e
        RaiseEvent HasError(Me, EventArgs.Empty)
    End Sub
    Public Function GetLastExcceptio() As Exception
        Return _lastRemoveException
    End Function
    Public Function RemoveCurrentTab(ByVal tp As TabPage) As Boolean
        Dim result As Boolean
        Try
            TabPages.Remove(tp)
            RemoveTabs.Add(tp)
            result = True
        Catch ex As Exception
            OnError(ex)
        End Try
        Return result
    End Function
    Public Function AddTabBack(ByVal title As String) As Boolean
        Dim result As Boolean
        Try
            If Not String.IsNullOrEmpty(title) Then
                For Each tp As TabPage In RemoveTabs
                    If String.Compare(tp.Text, title) = 0 Then
                        RemoveTabs.Remove(tp)
                        TabPages.Add(tp)
                        result = True
                        Exit For
                    End If
                Next
            End If
        Catch ex As Exception
            OnError(ex)
        End Try
        Return result
    End Function
#End Region

#Region "CONTROL_EVENTS"
    Private Sub MyTabControl_DoubleClick(sender As Object, e As EventArgs) Handles Me.DoubleClick
        RemoveCurrentTab(Me.SelectedTab)
    End Sub
#End Region

End Class





regs

ron O。



regs
ron O.


MSDN写道:

备注



该成员对此控件没有意义。



要隐藏TabControl中的选项卡,必须将其从控件的TabPages集合中删除。



来源: TabPage.Visible Property [ ^ ]



因此,一些成员建议使用非常难看的解决方案,其中两个TabControl放在同一个表单上TabPages在t之间移动下摆。

注意:我不推荐这个。而不是它,请按照以下链接:

向导 [ ^ ]

向导:设计指南 [ ^ ]


Source: TabPage.Visible Property [^]

So, some members recommend very ugly solution for this, where two TabControls are placed on the same form and TabPages are moved between them.
Note: I do NOT recommend this. Rather then it, follow these links:
Wizards[^]
Wizards: Design Guidelines[^]


这篇关于如何在TabControl中隐藏和显示标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆