Solved

VB.net - find if a control is visible or not

Posted on 2007-04-10
6
867 Views
Last Modified: 2009-07-29
I have a tab control with multiple tab pages.

I want to check if controls are visible on tab pages.  This works OK if I'm on the tab page that has the control I want to check is visible.
An example:

TabPage1 - has a control called Panel1. Panel1 is set to be visible/invisible by the program
TabPage2 - other stuff
TabPage3 - other stuff

If I am on TabPage2 or 3, and check if Panel1 on TabPage1 is visible, it always returns false (even though the Panel might be visible if I switch to TabPage1)

Basically I need to know if the control e.g. Panel1 is visible whether or not I'm on TabPage1.

Is there a way to know if Panel1 WOULD BE visible when switching to TabPage1 (even though it's NEVER visible to user when on a TabPage other than TabPage1)


Thanks
0
Comment
Question by:rwallacej
  • 3
  • 2
6 Comments
 

Author Comment

by:rwallacej
ID: 18881284
there are lots of showing/hiding of panels on Tabpage1. I'd hope not to have to set a variable every time the panel is set from visible to invisible
0
 
LVL 10

Expert Comment

by:tpwells
ID: 18881419
You can loop through the controls on a tabpage and check their visible state to see if any are visible this way:

        Dim myTabPage As New TabPage
        Dim controlsVisible As Boolean = False
        For Each c As Control In myTabPage.Controls
            If c.Visible Then
                controlsVisible = True
            End If
        Next

        If controlsVisible Then
            'there are visible controls on the tab page
        End If
0
 
LVL 10

Expert Comment

by:tpwells
ID: 18881427
Leave off the line:
        Dim myTabPage As New TabPage

and replace myTabPage with the name of the tab page you are wanting to check in this line:
        For Each c As Control In myTabPage.Controls
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rwallacej
ID: 18881522
this works....but only if myTabPage  is visible
I need to check if a control WILL BE visible if I were to switch to myTabPage if I am not already on it
0
 
LVL 34

Accepted Solution

by:
Sancler earned 350 total points
ID: 18881653
I don't think you can do that.  Try this.  One form, a panel, a few controls WITHIN the panel, a button OUTSIDE the panel.  This code

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Panel1.Visible = Not Panel1.Visible
        Debug.WriteLine("Panel visible = " & Panel1.Visible.ToString)
        For Each ctl As Control In Panel1.Controls
            Debug.WriteLine(ctl.Name & " visible = " & ctl.Visible)
        Next
    End Sub

As you can see if you run that, the toggling of the container control's visibility also toggles the visibility of the contained controls.

Although the TabPage control has no (overt) .Visible property, there is one.  The (2005) documentation says, in the list of properties

>>
 Visible  This member is not meaningful for this control.
<<

but if you click on the Visible link the documents say

>>
TabPage.Visible Property

[...]

This property supports the .NET Framework infrastructure and is not intended to be used directly from your code
<<

And if you stick code like this

        For Each ctl As Control In TabControl1.Controls
            If TypeOf (ctl) Is TabPage Then
                Debug.WriteLine(ctl.Name & " visible = " & ctl.Visible.ToString)
            End If
        Next

in a tabcontrol's SelectedIndexChanged sub, you will see that .Visible is set to False for all except the currently selected TabPage.

Putting those two little experiments together ... ;-(

Roger
0
 
LVL 10

Assisted Solution

by:tpwells
tpwells earned 150 total points
ID: 18881717
Sancler is right the controls inherit their visible state from their parent unless specifically told to be non-visible the control will be visible when the parent is visible and have visible=false when the parent is not visible.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now