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):

private
void 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.

[2528 byte] By [MikeJensen] at [2008-3-1]
# 1
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!

MikeJensen at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

Hello Mike,

There is a known issue with the TabControl class in the .NET Compact Framework v1.0: removing and adding back a tab page clears its Text property. In order to workaround this issue, the TabPage.Text property needs to be set to its original value after the tab page has been added back to the TabControl. This problem has been fixed in the .NET Compact Framework v2.0.

You could try to use the following method to swap two tab pages:

private void SwapTabPages(TabControl tc, TabPage tp1, TabPage tp2)

{

int index1;

int index2;

string text1;

string text2;

TabPage tp;

int index;

index1 = this.tabControl1.TabPages.IndexOf(tp1);

text1 = tp1.Text;

index2 = this.tabControl1.TabPages.IndexOf(tp2);

text2 = tp2.Text;

if ((index1 >= 0) && (index2 >= 0) && (index1 != index2))

{

if (index2 < index1)

{

tp = tp1;

tp1 = tp2;

tp2 = tp;

index = index1;

index1 = index2;

index2 = index;

}

// Remember current selection

index = tc.SelectedIndex;

tc.TabPages.RemoveAt(index2);

tc.TabPages.RemoveAt(index1);

((IList)tc.TabPages).Insert(index1, tp2);

tp2.Text = text2;

((IList)tc.TabPages).Insert(index2, tp1);

tp1.Text = text1;

if (index >= 0)

tc.SelectedIndex = index;

}

}

SergeyKuryata at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3

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;

}

MickDoherty at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
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.

MikeJensen at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...