Manoj Nayak
Manoj Nayak

Reputation: 2509

Searching for a tabpage in the tabcontrol C#

I have a tabcontrol in my application. I have a listbox which contains the line no of error and file name and path of the file.On double click i want to add the new tab page.The title of the tabpage should be the name of file from the listbox. If the tabpage with the particular filename already exists then it should not open new tabpage the cursor should point to that page. How to retreive the name of the tabpages .

private void lstErrorList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ArrayList errorType = new ArrayList();
            if (lstErrorList.Items.Count > 0)
            {
                string error = lstErrorList.SelectedItem.ToString();



                {

                    int result = error.LastIndexOf('\\');
                    string filename = error.Substring(result + 1, error.Length - (result + 1));
                    int pagecount;
                    TabPage tp = new TabPage();
                    pagecount = this.tabControl1.TabPages.Count;
                    for(int tbpagecount=0;tbpagecount<pagecount;tbpagecount++)
                    {
                        pagelist.Add(this.tabControl1.TabPages.ToString());
                    }
                    if (pagelist.Contains(filename))
                    {


                    }
                    else
                    {
                        this.tabControl1.TabPages.Insert(pagecount, filename);
                        pagecount++;
                    }

                    if (fileNamesList.Count == 0)
                        fileNamesList.Add(filename);
                    else
                    {
                        if (fileNamesList.Contains(filename))
                        {
                            //fileNamesList.Add("");
                        }
                        else
                        {
                            fileNamesList.Add(filename);

                        }

                    }
                }  

Upvotes: 9

Views: 17436

Answers (3)

Edy
Edy

Reputation: 71

        var tabPage = tabControl1.TabPages[filename];
        if (tabPage != null)
        {
            tabControl1.SelectedTab = tabPage;
        }
        else
        {
            tabControl1.TabPages.Add(filename, filename);
        }

Upvotes: 7

Jason
Jason

Reputation: 6926

What about something like this?

        string fileName = "";
        bool isPresent = false;

        for (int i = 0; i < tabControl1.TabPages.Count; i++)
        {
            if (tabControl1.TabPages[i].Name == filename)
            {
                isPresent = true;
                break;
            }
        }

        if (isPresent)
        {
                tabControl1.TabPages.Add(filename);
        }
        else
        {
                tabControl1.SelectTab(tab.TabIndex);
        }

Upvotes: 2

grimmig
grimmig

Reputation: 1421

        bool found = false;
        foreach (TabPage tab in tabControl1.TabPages) {
            if (filename.Equals(tab.Name)) {
                tabControl1.SelectedTab = tab;
                found = true;
            }
        }
        if( ! found)
                tabControl1.TabPages.Add(filename,filename);

Upvotes: 12

Related Questions