Click here to Skip to main content
15,886,012 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I got a windows form with Datagridview including Employee information. when i select say one Row from dridvie and Click on New Button it loads a new Tabcontrol which Title employee name.

I have implemented so every time user select one row from grid view and click on new Button it adds new tab control with name of Tabpage employee name. when employee already exists it do now open Particular tab which already Opens thats how i want and it works perfectly fine.

But when i select a row from gridview Employee which Tab is already open and click on new it doesnt open new tab which is ok but if it is already open every time i click new i want to go inside that Tab page How to do it???

Here is my code
SQL
private void button_New_Click(object sender, EventArgs e) 
{ 
TabPage tpEmployee = new TabPage(emp.Vorname);
tpEmployee.Tag = emp.ID;
            GUI.Ucemp ucempTes = new GUI.UCemp();
             Ucemptest.Employee = emp;
             Ucemptest.Dock = DockStyle.Fill;
            tpEmployee.Controls.Add(ucempTes);

            Boolean found = false;


            foreach (TabPage tp in tabControl.TabPages)
            {
                if (tp.Tag != null && tp.Tag.Equals(tpEmployee.Tag))
                {
                    found = true;
                 //Here i want to go inside Tab which is already open
                 //i also tried
                 //tabControl.SelectedTab = tpEmployee;
                // But it is blank
               }
            }

            if (!found)
            {

                this.tabControl.TabPages.Add(tpEmployee);
                tabControl.SelectedTab = tpEmployee;


            }
Posted
Updated 20-Nov-14 22:29pm
v3

This is based on the idea that you should only create a new object (TabPage) when you require one:
C#
private void button_New_Click(object sender, EventArgs e) 
{ 
    foreach (TabPage tp in tabControl.TabPages)
    {
        if (tp.Tag != null && tp.Tag.Equals(tpEmployee.Tag))
        {
            //Here i want to go inside Tab which is already open
            
            tabControl.SelectedTab = tp;
            tp.Focus();
            
            // ? set Focus on a Control you have a reference to inside the TabPage
            // tp.Controls["ucempTes"].Focus();
    
            // done
            return;    
       }
    }
    
    TabPage tpEmployee = new TabPage(emp.Vorname);
    tpEmployee.Tag = emp.ID;
    GUI.Ucemp ucempTes = new GUI.UCemp();
    Ucemptest.Employee = emp;
    Ucemptest.Dock = DockStyle.Fill;
    tpEmployee.Controls.Add(ucempTes);
}
If you can get a reference to the 'ucempTes inside the TabPage, and you want to set force Focus to a Control inside the 'ucempTes, like a TextBox:
C#
tp.Controls["ucempTes"].Controls["textBox1"].Focus();
tp.Controls["ucempTes"].Controls["textBox1"].Capture = true;
This tedious repetition of dot-notation access can be eliminated by creating a Dictionary mapping the object you wish to focus on in each TabPage to the DataGridView rows.
 
Share this answer
 
C#
foreach (TabPage tabpage in tabControl.TabPages)
                {
                    
                    if (tabpage.Tag != null && tabpage.Tag.Equals(tpBewerber1.Tag))
                    {
              
                      
                       int index = tabControl.TabPages.IndexOf(tpEmp);
                         //index is always 0 but i got 3 tabpages
                         tabControl.SelectedTab = tpBewerber1;
                      }
 
Share this answer
 
v2

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