Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Improving on the good solution by Hans Passant I decided to write an extension method based on his solution and adding other stuff as well. I am surprised that even in .NET 4 this basic functionality hasn't been fixed.</p> <ul> <li>Implemented it as an Extension Method that can be reused in a more transparent manner</li> <li>The clean up method only cleans up the pages of the control being disposed/cleaned up.</li> <li>Whenever possible the tab page is restored to its same position. This isn't always possible if you hide/show several tab pages.</li> <li>It does some error and parameter checking</li> <li>To make it invisible it finds out its parent. When making visible it has to be given because the Parent property is null when the tab page has been removed.</li> </ul> <p><br/> </p> <pre><code>public static class TabPageExtensions { private struct TabPageData { internal int Index; internal TabControl Parent; internal TabPage Page; internal TabPageData(int index, TabControl parent, TabPage page) { Index = index; Parent = parent; Page = page; } internal static string GetKey(TabControl tabCtrl, TabPage tabPage) { string key = ""; if (tabCtrl != null &amp;&amp; tabPage != null) { key = String.Format("{0}:{1}", tabCtrl.Name, tabPage.Name); } return key; } } private static Dictionary&lt;string, TabPageData&gt; hiddenPages = new Dictionary&lt;string, TabPageData&gt;(); public static void SetVisible(this TabPage page, TabControl parent) { if (parent != null &amp;&amp; !parent.IsDisposed) { TabPageData tpinfo; string key = TabPageData.GetKey(parent, page); if (hiddenPages.ContainsKey(key)) { tpinfo = hiddenPages[key]; if (tpinfo.Index &lt; parent.TabPages.Count) parent.TabPages.Insert(tpinfo.Index, tpinfo.Page); // add the page in the same position it had else parent.TabPages.Add(tpinfo.Page); hiddenPages.Remove(key); } } } public static void SetInvisible(this TabPage page) { if (IsVisible(page)) { TabControl tabCtrl = (TabControl)page.Parent; TabPageData tpinfo; tpinfo = new TabPageData(tabCtrl.TabPages.IndexOf(page), tabCtrl, page); tabCtrl.TabPages.Remove(page); hiddenPages.Add(TabPageData.GetKey(tabCtrl, page), tpinfo); } } public static bool IsVisible(this TabPage page) { return page != null &amp;&amp; page.Parent != null; // when Parent is null the tab page does not belong to any container } public static void CleanUpHiddenPages(this TabPage page) { foreach (TabPageData info in hiddenPages.Values) { if (info.Parent != null &amp;&amp; info.Parent.Equals((TabControl)page.Parent)) info.Page.Dispose(); } } } </code></pre>
 

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