Check Control.Visible property value

N

news.microsoft.com

Hi,


How can I check if my control is visible at run-time BEFORE the form is
fully loaded?



In design mode you can create a window and put a Button inside, set the
property Visible to true.



But at run-time at least the Button is fully visible this property will
return false.



I need to know what is the "status" of this button before the Form (Parent)
and itself are visible.



In my case I have to do some logic in the Form Constructor, then if the
button "is visible design mode" then do something if not do something else.



Basically in the constructor you will always get a Visible = false although
the property is set to TRUE.



For example in ToolStrip you get a property "Available". It tell if the
control should be render or not, that it is not the same as Visible, and
that is what I'm looking for in a Control object.



Thanks,

Gustavo.
 
S

Stoitcho Goutsev \(100\)

This is true.
In design time the control designer shadows the Visible proeprty, that is it
keeps separate the value the user sets and the actually value set for the
control, thus reading the visible property at design time will return always
*true* of you want to read the value the user has set you need to go
throught the property's ProeprtyDescriptor -
TypeDescriptor.GetProeprties(....)["Visible"].GetValue(...)

At run time though both methods (reading the property and using the property
descriptor) will return the actual visible state of the control, which will
be *false* during the form load.
 
N

news.microsoft.com

Thanks for the answer, but I tried with TypeDescriptor and still I'm not
getting the result I expect.

My problem is in Run-Time and not in Design Time. I need to know what is the
Visible in the constructor after InitializeComponent() is called.

When I try the following I'm getting both results (statusA and StatusB)
equal to False, and the reality is that button1.Visible was set to True
inside InitializeComponent();

button1.Visible gives true only after the Form and Button are show.

public class FormTest
{
public FormTest()
{
InitializeComponent(); // Here it creates a button (button1) and
makes it visible

bool statusA = button1.Visible;
PropertyDescriptor property =
TypeDescriptor.GetProperties(button1)["Visible"];
bool statusB = (bool) property.GetValue(button1);
}
....
}

I think you response was kind of to know the real status in Design Time but
what I need is at Run-Time.

Thanks,
Gustavo.

Stoitcho Goutsev (100) said:
This is true.
In design time the control designer shadows the Visible proeprty, that is
it keeps separate the value the user sets and the actually value set for
the control, thus reading the visible property at design time will return
always *true* of you want to read the value the user has set you need to
go throught the property's ProeprtyDescriptor -
TypeDescriptor.GetProeprties(....)["Visible"].GetValue(...)

At run time though both methods (reading the property and using the
property descriptor) will return the actual visible state of the control,
which will be *false* during the form load.


--
HTH
Stoitcho Goutsev (100)

news.microsoft.com said:
Hi,


How can I check if my control is visible at run-time BEFORE the form is
fully loaded?



In design mode you can create a window and put a Button inside, set the
property Visible to true.



But at run-time at least the Button is fully visible this property will
return false.



I need to know what is the "status" of this button before the Form
(Parent) and itself are visible.



In my case I have to do some logic in the Form Constructor, then if the
button "is visible design mode" then do something if not do something
else.



Basically in the constructor you will always get a Visible = false
although the property is set to TRUE.



For example in ToolStrip you get a property "Available". It tell if the
control should be render or not, that it is not the same as Visible, and
that is what I'm looking for in a Control object.



Thanks,

Gustavo.
 
S

Stoitcho Goutsev \(100\)

That I was trying to say. In the form constructor you will get always
visible = false because the controls are indeed not visible on the screen.
Visible flag gives you the actuall state of the control.
bool statusA = button1.Visible;
PropertyDescriptor property =
TypeDescriptor.GetProperties(button1)["Visible"];
bool statusB = (bool) property.GetValue(button1);

In runtime both methods of reading the value are equivalent in this case.


--

Stoitcho Goutsev (100)

Stoitcho Goutsev (100) said:
This is true.
In design time the control designer shadows the Visible proeprty, that is
it keeps separate the value the user sets and the actually value set for
the control, thus reading the visible property at design time will return
always *true* of you want to read the value the user has set you need to
go throught the property's ProeprtyDescriptor -
TypeDescriptor.GetProeprties(....)["Visible"].GetValue(...)

At run time though both methods (reading the property and using the
property descriptor) will return the actual visible state of the control,
which will be *false* during the form load.


--
HTH
Stoitcho Goutsev (100)

news.microsoft.com said:
Hi,


How can I check if my control is visible at run-time BEFORE the form is
fully loaded?



In design mode you can create a window and put a Button inside, set the
property Visible to true.



But at run-time at least the Button is fully visible this property will
return false.



I need to know what is the "status" of this button before the Form
(Parent) and itself are visible.



In my case I have to do some logic in the Form Constructor, then if the
button "is visible design mode" then do something if not do something
else.



Basically in the constructor you will always get a Visible = false
although the property is set to TRUE.



For example in ToolStrip you get a property "Available". It tell if the
control should be render or not, that it is not the same as Visible, and
that is what I'm looking for in a Control object.



Thanks,

Gustavo.
 
N

news.microsoft.com

Then basically I cannot get the Visible property before the form is
displayed, that sucks.

Gustavo.

Stoitcho Goutsev (100) said:
That I was trying to say. In the form constructor you will get always
visible = false because the controls are indeed not visible on the screen.
Visible flag gives you the actuall state of the control.
bool statusA = button1.Visible;
PropertyDescriptor property =
TypeDescriptor.GetProperties(button1)["Visible"];
bool statusB = (bool) property.GetValue(button1);

In runtime both methods of reading the value are equivalent in this case.


--

Stoitcho Goutsev (100)

Stoitcho Goutsev (100) said:
This is true.
In design time the control designer shadows the Visible proeprty, that
is it keeps separate the value the user sets and the actually value set
for the control, thus reading the visible property at design time will
return always *true* of you want to read the value the user has set you
need to go throught the property's ProeprtyDescriptor -
TypeDescriptor.GetProeprties(....)["Visible"].GetValue(...)

At run time though both methods (reading the property and using the
property descriptor) will return the actual visible state of the
control, which will be *false* during the form load.


--
HTH
Stoitcho Goutsev (100)

message Hi,


How can I check if my control is visible at run-time BEFORE the form is
fully loaded?



In design mode you can create a window and put a Button inside, set the
property Visible to true.



But at run-time at least the Button is fully visible this property will
return false.



I need to know what is the "status" of this button before the Form
(Parent) and itself are visible.



In my case I have to do some logic in the Form Constructor, then if the
button "is visible design mode" then do something if not do something
else.



Basically in the constructor you will always get a Visible = false
although the property is set to TRUE.



For example in ToolStrip you get a property "Available". It tell if the
control should be render or not, that it is not the same as Visible,
and that is what I'm looking for in a Control object.



Thanks,

Gustavo.
 
L

Lee

Gustavo:

I'm pretty sure that after InitializeComponent completes, the Visible
property of your controls will be populated the way they will be at
run-time. Therefore, if you create your own New method [with a
different signature than the default New()], you should be able to
specify your start-up logic after it invokes InitializeComponent.
 
S

Stoitcho Goutsev \(100\)

As far as I understand, Gustavo wants to read the value which is set in the
IntiazlizeComponent.
For example if at design time a button is set to have Visible = true he
wants to read *true* from the button's visible property at run time (in the
constructor after InitiazlizeComponent), wich is not posible (using all
managed code) because the Visible returns the real state of the controls,
which at this point of execution is *false* because the form and all its
controls are not shown yet.

I said it is not possible using managed code, it doesn't mean it is not
possible at all.
I see two solution to the problem: one using PInvoke and looking for
control's WS_VISIBLE style and the other is using reflections and reading
controls internal state. If I must suggest one of them I'd probably give my
preferences to the PInvoke one because it doesn't use anything not
documented, while the reflection one is a hack even though it should work on
all current versions of windows forms.

PInvoke:
---------
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
private const int GWL_STYLE = -16;
private const int WS_VISIBLE = 0x10000000;
bool IsControlSetVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
{
ctrl.CreateControl();
}
int ctrlStyles = GetWindowLong(ctrl.Handle, GWL_STYLE);
return ((ctrlStyles & WS_VISIBLE) != 0);

}

Reflection method:
------------------
private bool IsControlSetVisible2(Control ctrl)
{
Type t = typeof(Control);
MethodInfo mi = t.GetMethod("GetState", BindingFlags.Instance |
BindingFlags.NonPublic);
if (mi == null)
return false;

return (bool)mi.Invoke(ctrl, new object[] { 2 });

}
 
N

news.microsoft.com

Excellent news,

I'll use Win API to query WS_VISIBLE, Reflection is kind of invasive.

Thanks very much for the answers guys.
Gustavo.

Stoitcho Goutsev (100) said:
As far as I understand, Gustavo wants to read the value which is set in
the IntiazlizeComponent.
For example if at design time a button is set to have Visible = true he
wants to read *true* from the button's visible property at run time (in
the constructor after InitiazlizeComponent), wich is not posible (using
all managed code) because the Visible returns the real state of the
controls, which at this point of execution is *false* because the form
and all its controls are not shown yet.

I said it is not possible using managed code, it doesn't mean it is not
possible at all.
I see two solution to the problem: one using PInvoke and looking for
control's WS_VISIBLE style and the other is using reflections and reading
controls internal state. If I must suggest one of them I'd probably give
my preferences to the PInvoke one because it doesn't use anything not
documented, while the reflection one is a hack even though it should work
on all current versions of windows forms.

PInvoke:
---------
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
private const int GWL_STYLE = -16;
private const int WS_VISIBLE = 0x10000000;
bool IsControlSetVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
{
ctrl.CreateControl();
}
int ctrlStyles = GetWindowLong(ctrl.Handle, GWL_STYLE);
return ((ctrlStyles & WS_VISIBLE) != 0);

}

Reflection method:
------------------
private bool IsControlSetVisible2(Control ctrl)
{
Type t = typeof(Control);
MethodInfo mi = t.GetMethod("GetState", BindingFlags.Instance |
BindingFlags.NonPublic);
if (mi == null)
return false;

return (bool)mi.Invoke(ctrl, new object[] { 2 });

}



--
HTH
Stoitcho Goutsev (100)

Lee said:
Gustavo:

I'm pretty sure that after InitializeComponent completes, the Visible
property of your controls will be populated the way they will be at
run-time. Therefore, if you create your own New method [with a
different signature than the default New()], you should be able to
specify your start-up logic after it invokes InitializeComponent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top