Solved

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

Posted on 2007-04-10
6
1,030 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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