Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to hide a toolbox associated with a UserControl in .NET 2.0?
    primarykey
    data
    text
    <p>I've recently come across the following situation. I have a <code>UserControl</code> that can show a modeless toolbox. If the user has shown the toolbox, I'd like to hide and show it appropriately as the <code>UserControl</code> itself becomes invisible or visible, respectively. The <code>UserControl</code> can be embedded in an arbitrary number of parent containers, such as a <code>GroupBox</code> or <code>TabPage</code>, which can influence whether or not the <code>UserControl</code> is actually visible despite its own <code>Visible</code> property being <code>True</code>.</p> <p>In WPF, it seems like I could use the <code>IsVisibleChanged</code> event of the <code>UserControl</code> to handle this. Is there an equivalent for WinForms? What would a general solution in .NET 2.0 be?</p> <p>EDIT: Here is the solution that I arrived at. Is there a better solution?</p> <pre><code>public partial class MyControl : UserControl { private List&lt;Control&gt; _ancestors = new List&lt;Control&gt;(); private bool _isVisible = false; public MyControl() { InitializeComponent(); ParentChanged += OnParentChanged; VisibleChanged += OnVisibleChanged; } private void OnParentChanged(object sender, EventArgs e) { foreach (Control c in _ancestors) { c.ParentChanged -= OnParentChanged; c.VisibleChanged -= OnVisibleChanged; } _ancestors.Clear(); for (Control ancestor = Parent; ancestor != null; ancestor = ancestor.Parent) { ancestor.ParentChanged += OnParentChanged; ancestor.VisibleChanged += OnVisibleChanged; _ancestors.Add(ancestor); } } private void OnVisibleChanged(object sender, EventArgs e) { bool isVisible = Visible; foreach (Control c in _ancestors) { if (!c.Visible) { isVisible = false; break; } } if (isVisible != _isVisible) { _isVisible = isVisible; // Control visibility has changed here // Do something } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload