My usercontrol inherits System.Windows.Forms.Control
class. The following link describes the \"Visible\" property of control
Control.Visible
All controls in windows forms internally maintain their state. Visibility is also one of the things they maintain in state. Because it helps to identify why visibility of the control was changed.
Control.Visible
will return false if there is a control above your control or parent of your control is hidden. But value of Visible property in state will be false only if user set it to false.
Code:
//Method to ensure the visibility of a control
public bool DetermineVisibility(Control control)
{
//Avoid reflection if control is visible
if (control.Visible)
return true;
//Find non-public GetState method of control using reflection
System.Reflection.MethodInfo GetStateMethod = control.GetType().GetMethod("GetState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//return control's visibility if GetState method not found
if (GetStateMethod != null)
//return visibility from the state maintained for control
return (bool)(GetStateMethod.Invoke(control, new object[] { 2 }));
return false;
}