Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi fellow programmers i have a Window form that contains a TabControl which is empty at runtime, i also have a listbox with name of form associated to a user control, when selecting the name from the listbox, i want to be able to check if the tabpage has already been opened before, if yes it does nothing but if that tab has not been opened it opens the new tab.. So far i have tried to compare the text of the selected item in the listbox to the titles of the tabpages already open, but it does not work... What should i do.. Thanks
Posted

1 solution

Hello,

Hope this will help you.. I have a windows form in that Tabcontrol1 is placed. The list box has five values "one", "two","three","four","five". Whenever I select an item in the list box the code will check for the corresponding tabpage is opened or not. If tabpage is not there it will create a tab page and add it to TabPage1. Otherwise if the page is already there it will select the particular page and return. Initially set the tabpages to 0. Hope this helps you to meet your requirement..

C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
       {

           foreach (TabPage tp in tabControl1.TabPages)
           {
               //check for tag tile with the selected item in the list box
               if (tp.Text == listBox1.SelectedItem.ToString())
               {
                   //select the particular tabpage
                   tabControl1.SelectedTab = tp;
                   return;
               }
           }
           //if the tabpage is not found in the existing tabs it has to be created
           //crate a new tab
           TabPage newPage = new TabPage(listBox1.SelectedItem.ToString());
           tabControl1.TabPages.Add(newPage);

       }
 
Share this answer
 
Comments
IamBlessed 30-May-12 8:22am    
Thanks you very much.. Just wanted to know do you know of any custom tabcontrol dll that has a close button
Jim Jos 30-May-12 8:28am    
You just put a button and close the tag when it's clicked.. Closing a tabpage is just removing from the collection.. TabPages.Remove
MugiwaraUK 30-May-12 8:30am    
The only thing to add here is the content for the tab page

var nameOfControl = controlToAddToTabPage
newPage.Controls.Add(nameOfControl);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900