1

Hi guys I'm trying to change the order of tabpages but whatever I do I fail.

I have a total of 24 tabpages and what I would like to do is

keep the first 5 tabpages as it is ( index 0 to 4 will remain same) and move the pages 16 to 23 (index 15 to 22) at index 5

so it should look something like this:

index:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

page number:

1 2 3 4 5 16 17 18 19 20 21 22 23 6 7 8 9 10 11 12 13 14 15 24

to accomplish this task I tried to following code:

for (int i = 23; i >= 16; i--)
          {
              TabPage loTabPage;
              loTabPage = this.myCustomTab.TabPages[i];
              this.myCustomTab.TabPages.RemoveAt(i);
              this.myCustomTab.TabPages.Insert(5, loTabPage);
          }

but the above code fails, any suggestions?

Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42

4 Answers4

2

Your backwards iteration makes it a moving target, and is largely unnecessary since all you really want to do is move eight tabs from the back to index five, so:

for (int i = 0; i < 8; ++i) {
  TabPage tp = tabControl1.TabPages[22];
  tabControl1.TabPages.RemoveAt(22);
  tabControl1.TabPages.Insert(5, tp);
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Hi sir this helped me a lot, on my console I can see that the order of the pages is as I want them to be however on the program window, the inserted tabs are not visible any Idea why? – Asım Gündüz Dec 16 '15 at 08:17
  • @user3456351 They are visible on my screen, so I don't know what your code looks like that would cause that. – LarsTech Dec 16 '15 at 14:39
1

Your problem is that the item you need to move on each iteration is always the last item. So instead of

loTabPage = this.myCustomTab.TabPages[i];
this.myCustomTab.TabPages.RemoveAt(i);

It should be:

loTabPage = this.myCustomTab.TabPages[23];
this.myCustomTab.TabPages.RemoveAt(23);

When you insert, the item that was at 22 is now going to be pushed to 23.

Of course, it would be much more efficient to remove the whole block in one operation and insert it again rather than going 1 by 1.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
1

Have no a compilator now, but expect something like this to work:

var tabs = new List<TabPage>();
tabs.AddRange(myCustomTab.TabPages/*.OfType(TabPage)*/);
var indexes = new int[] {0,1,2,3,4,15,16,17,18,19,20,21,22,5,6,7,8,9,10,11,12,13,14,23};
for (var tab in tabs) tab.Parent = null;
for (var i in indexes) tabs[i].Parent = myCustomTab;

If no, please leave a comment and I'll delete the answer.

Qwertiy
  • 19,681
  • 15
  • 61
  • 128
0

I find Linq produces very legible code for this sort of task. This works for me:

var tabs = tabControl.TabPages.OfType<TabPage>().ToArray();
var bringForward = tabs.Skip(15).Take(8);

tabs = tabs.Take(5)
           .Union(bringForward)
           .Union(tabs)
           .ToArray();

tabControl.TabPages.Clear();
tabControl.TabPages.AddRange(tabs);

(You'll need a using System.Linq; amongst your using statements, naturally.)

So after using Linq to select just the tabs we want to bring forward, in order, we can then build an array very legibly. We rely on Union()'s property of selecting distinct elements, i.e. not adding duplicates.

El Zorko
  • 3,349
  • 2
  • 26
  • 34