detecting control visibility before form shows

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Is it possible to tell if a control would be visible (or hidden) before
making its parent form visible?

Bob
 
Bob,

Nothing will show Visible until the form itself has shown itself. You can
test this by setting up two forms. Form1 has just a button called button1.
Form2 has a textBox1 and a button1. The button was created first and holds
the position of 0 in the Form.Components array. textBox1 will hold the value
of 1 in the Form.Components array. By setting the Visible to true on the
textBox1 control and placing the following code in the button1_Click event
you will always get textBox1 is NOT visible.

private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2 = new Form2();

switch (f2.Controls[1].Visible)
{
case true:
MessageBox.Show("textBox1 is visible", "Form1");
break;
case false:
MessageBox.Show("textBox1 is NOT visible", "Form1");
break;
}
f2.Show();
}

If you are looking to set it when the form loads based on roles and security
your best bet is being able to set a variable on the form that tells the Load
event what you want on and off on the form based on the users credentials.
This is of course assuming that is the direction you are going with this.

Regards,

Kamaluokeakua
 
Nope, not the direction I was going.

Let's see... it will be easier for me to start with the example I'm working
on at the moment. Picture a form where a user can select from a list of
items. When this list's selected item changes, the containing form handles
this as an event and notifies many other controls - all of which are on
various tab pages - that the selected item has changed. These controls, on
being notified, need to reload data based on this new parameter. As you
might guess, in the interest of making forms of such design more efficient,
I cause these data-loading controls to check first to see if they're
available to the user in the first place. If, for instance, a control finds
an unselected tab page in its chain of parents, it can set a local
'LoadDeferred' bit, and defer loading until OnVisibleChanged.

Simply checking for unselected tab pages in a control's parent chain does do
the trick for a lot of cases. But I want to solve for the general case; if
the user isn't going to see the control, I want it to hold off loading data
until it becomes visible. Using the Visible property is what I would want to
use, but this doesn't work if I want to make a selection (load a saved
state) before the form is even shown. I can't use an 'is this control
currently visible' property, I need an independent 'was this control told to
be visible in this parent' property.

Does that make any sense?

Anyway, there are hacks around this problem, and I don't exactly _have_ to
load a form's saved state before it becomes visible. But that looks messier,
causes a double-loading of data, and I *know* that hierarchal Visible
information is available somewhere, because the WinForms uses it to give you
an end result. I just need to know how to get at it. Perhaps there's an API
call?

Bob

Nothing will show Visible until the form itself has shown itself. You can
test this by setting up two forms. Form1 has just a button called button1.
Form2 has a textBox1 and a button1. The button was created first and holds
the position of 0 in the Form.Components array. textBox1 will hold the value
of 1 in the Form.Components array. By setting the Visible to true on the
textBox1 control and placing the following code in the button1_Click event
you will always get textBox1 is NOT visible.

private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2 = new Form2();

switch (f2.Controls[1].Visible)
{
case true:
MessageBox.Show("textBox1 is visible", "Form1");
break;
case false:
MessageBox.Show("textBox1 is NOT visible", "Form1");
break;
}
f2.Show();
}

If you are looking to set it when the form loads based on roles and security
your best bet is being able to set a variable on the form that tells the Load
event what you want on and off on the form based on the users credentials.
This is of course assuming that is the direction you are going with this.
 
Back
Top