Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a tabpage in the tabcontrol C#

Tags:

c#

winforms

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

                        }

                    }
                }  
like image 329
Manoj Nayak Avatar asked Feb 28 '12 06:02

Manoj Nayak


2 Answers

        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);
like image 64
grimmig Avatar answered Nov 08 '22 02:11

grimmig


        var tabPage = tabControl1.TabPages[filename];
        if (tabPage != null)
        {
            tabControl1.SelectedTab = tabPage;
        }
        else
        {
            tabControl1.TabPages.Add(filename, filename);
        }
like image 27
Edy Avatar answered Nov 08 '22 03:11

Edy