C# Storing original positions of Tab Pages within a Tab Control
I'm relatively new to C# and I'm trying to use a tab control which has 5 tab pages within it. These tab pages are displayed and hidden when required and I开发者_Python百科 am able to re-add pages to the required position e.g. tabPage 2 should be re-added between tabPage1 and tabPage3 by passing in a number relating to the position I want it to appear in and swapping the pages around. How do I store the original positions of the tabPages and then just say tabPage2 should be added in to tabPage2's stored position?
Thanks in advance for any help.
You could store the original position in the Tag property. Some logic is required because neither page 1 nor 3 might be present. This ought to be close:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
for (int page = 0; page < tabControl1.TabCount; ++page)
tabControl1.TabPages[page].Tag = page;
}
private List<TabPage> hiddenPages = new List<TabPage>();
public void ShowTab(TabPage page) {
int pos = (int)page.Tag;
int insertPoint;
for (insertPoint = 0; insertPoint < tabControl1.TabCount; ++insertPoint) {
if (pos <= (int)tabControl1.TabPages[insertPoint].Tag) break;
}
tabControl1.TabPages.Insert(insertPoint, page);
hiddenPages.Remove(page);
}
}
精彩评论