473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using a loop to remove tabpages from my tabcontrol [EASY]

Hi there

Basically, when a user logs out of my system I am trying to use a loop to loop through all the tabs in the tabcontrol and remove them, before adding the login tab back on there. Here's my code:

Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show(Convert.ToString(tabControl1.TabCount));
  2.             for (int i = 0; i <= tabControl1.TabCount; i++)
  3.             {
  4.                 tabControl1.TabPages.RemoveAt(i);
  5.             }
  6.             logoutlabel.Visible = false;
  7.             FirstLogin();
Basically, I am using the messagebox to check the number of tabs (Just to make sure I'm not going insane) and then I loop through the tabs attempting to remove them.

I get the "index [number] is out of range". Sometimes it'll remove the last two tabs, sometimes it'll just error straight away. I'm not sure what I'm doing wrong here. Please help me out!
Mar 1 '11 #1

✓ answered by Alex Dransfield

Revised it! Silly me, I didn't think that after index 0 has been removed, the previous index 1 becomes the new index 0!

This code fixes it.

Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show(Convert.ToString(tabControl1.TabCount));
  2.             for (int i = tabControl1.TabCount-1; i >=0; i--)
  3.             {
  4.                 tabControl1.TabPages.RemoveAt(i);
  5.             }
  6.             logoutlabel.Visible = false;
  7.             FirstLogin();

2 3997
Revised it! Silly me, I didn't think that after index 0 has been removed, the previous index 1 becomes the new index 0!

This code fixes it.

Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show(Convert.ToString(tabControl1.TabCount));
  2.             for (int i = tabControl1.TabCount-1; i >=0; i--)
  3.             {
  4.                 tabControl1.TabPages.RemoveAt(i);
  5.             }
  6.             logoutlabel.Visible = false;
  7.             FirstLogin();
Mar 1 '11 #2
PaulBa
1
why not just clear tabs, if you want to delete all of them?
Expand|Select|Wrap|Line Numbers
  1. tabControl1.TabPages.Clear();
Jan 16 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: a | last post by:
I'm trying to delete an item from a collection, by clicking on a delete button in a GridView control. The item in the collection is stored in the Profile object (serialized as xml in the...
7
by: a | last post by:
If the code to insert a new Student is: Profile.Teachers.Classes.Students.Add(new TCS.Student(id,teacher, class, name)); what is the code to Remove a student? I tried the code below, but I...
1
by: Rejimonb | last post by:
Can I fill a combobox without using a loop. Using a loop will be time consuming. Reji
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
1
by: python101 | last post by:
I would like to use loop 'for' to make something look like ------ * * * * * * * * * * #(please don't mind ----- because I can not type the first star in right position) however I...
1
by: bhavanirayala | last post by:
Hi, How can I get the hash values without loop? i.e I want to assign the value of a first key to one variable based on the condition.If that condition falis i need to assign the second value...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
1
by: xukaifeng | last post by:
Hi guys, I retrieve books from a database and put each book with its data into a bean. this works well, the beans are called bookBeanBeg1, bookBeanBeg2, bookBeanBeg3, bookBeanBeg4, etc. ...
0
by: sksmart | last post by:
I have added httpmodule but when I am trying to remove it in subdirectory web.config of subdirectory it still get executed. Public Sub Init(ByVal context As System.Web.HttpApplication) Implements...
5
by: dave816 | last post by:
Sorry for the Excel question in an Access forum...................I don't see an Excel forum and there's probably a reason for that but figured I'd give this a shot anyway. Again sorry, delete if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.