Solved

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

Posted on 2007-04-10
6
902 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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