I was wondering about the difference between using a Control’s Hide()
method compared to setting the Visible
property to false.
When would I want to use the one over the other?
When you set Visible = false to a control, it is not rendered. That means there is no HTML representation of that control sent to the page.
Description. The Visible property lets you determine or set whether the form is visible on screen. To display the form on screen, set the Visible property to True or use the Show or ShowModal method. To hide the form, set Visible to False or use the Hide method.
To hide an object when printing, use the DisplayWhen property. You can use the Visible property to hide a control on a form or report by including the property in a macro or event procedure that runs when the Current event occurs.
They are equivalent. From the documentation for Control.Hide:
Hiding the control is equivalent to setting the Visible property to false.
You can confirm this with reflector:
public void Hide()
{
this.Visible = false;
}
You might use Show() or Hide() when you know the value and use Visible when you take the visibility in as a parameter, although personally I would always use Visible.
Use whatever you like, Hide()
or Visible
, but I can't find any reason to prefer one of them except if you are trying to check the control visibility status, so you should say if(pic.Visible)
and in this case you can't use the method Hide()
you should use the property Visible
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With