Tab Control swap difference in CF?
I'm working on my first CF app, and am finding differences between CF and non-CF handling of the tab control. I've searched and not found any reference to this item, so am hoping to find out if it is a bug or just my misunderstanding.
I've created two test apps. One is a ppc2003 .net CF app, the other is a windows .net app. They have a single form which contains a single tab control. This tab control has 3 tabpages defined in the designer. One of these tabpages has an 'add' button. When clicked a new TabPage is created with a single label and added to the tab control. This is then 'swapped' with the last tab page, ala 'inserting' it before the last tabpage. So the insert is like this:
tabPages.Controls.Add(addedPage);
SwapTabPages(addedPage,tabPage3);
Here's the SwapTabPages (courtesy ofhttp://dotnetrix.co.uk/tabcontrols.html):
privatevoid SwapTabPages(TabPage tp1, TabPage tp2)
{
if (tabPages.TabPages.Contains(tp1) ==false || tabPages.TabPages.Contains(tp2) ==false)
thrownew ArgumentException("TabPages must be in the TabControls TabPageCollection.");
int Index1 = tabPages.TabPages.IndexOf(tp1);
int Index2 = tabPages.TabPages.IndexOf(tp2);
tabPages.TabPages[Index1] = tp2;
tabPages.TabPages[Index2] = tp1;
}This works fine in the windows .net app, but in the CF app, the tab pagecontent swaps, but the tab Text does not on the device. But when iterating through the TabControl.TabPages, the added page is in the correct index, as if the swap really worked. The .text property is the correct one, etc. But the display doesn't show it.
Again, this works fine in the full .net frameworks. Is this a known problem in the CF or am I not getting the display to somehow refresh correctly?
Visual studios 2003, .net framework 1.1 w/sp1.
Another difference I'm finding between CF and full .Net. A work around may be to not 'swap' the pages by changing the index order in the TabControl, but to simply clear the control and readd in the order you'd like. But I'm finding differences here as well.
If I simply clear and readd the pages, they behave differently. It works just fine with the full .Net, but with CF, the .Text field is lost. The content on the TabPage is still there. This seems related to the above issue.
In case it matters, here is the simple code.
// has just three tab pages initially, tabPage1, tabPage2 and tabPage3
tabPages.Controls.Clear();
tabPages.Controls.Add(tabPage1);
tabPages.Controls.Add(tabPage2);
tabPages.Controls.Add(tabPage3);Thank you!
Hi Mike,
I hadn't tried this out in the CF and so was not aware of this bug. Heres a modified version of the routine which overcomes the issue. I'll update the example on my site to include this fix.
private void SwapTabPages(TabPage tp1, TabPage tp2)
{
if (tabPages.TabPages.Contains(tp1) == false || tabPages.TabPages.Contains(tp2) == false)
throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");
int Index1 = tabPages.TabPages.IndexOf(tp1);
int Index2 = tabPages.TabPages.IndexOf(tp2);
tabPages.TabPages[Index1] = tp2;
tabPages.TabPages[Index2] = tp1;
tabPages.SelectedIndex = tabPages.SelectedIndex;
string tp1Text, tp2Text;
tp1Text = tp1.Text;
tp2Text = tp2.Text;
tp1.Text=tp2Text;
tp2.Text=tp1Text;
}
Thank you two for your help. I have marked both response as answers, though I'll use the second swap example. The first one didn't swap the tab text.
The information was very helpful, I appreciate it.