Solved

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

Posted on 2007-04-10
6
832 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rwallacej
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

10 Experts available now in Live!

Get 1:1 Help Now