DarkPh03n1X
DarkPh03n1X

Reputation: 680

Multiple Tab Controls in one SelectedIndexChanged event

I have tab controls inside tab controls like the following: enter image description here .

I use to have 3 SelectedIndexChanged events but i combined them into one.

    private void MaintabControler_SelectedIndexChanged(object sender, EventArgs e)
    {
        _currentGlobalTable = "None Selected";
        string SelectedTab = tabControl1.SelectedTab.Name.ToString();

        // Contacts_Table
        if (SelectedTab == "mainTab1") 
        {
            this.contacts_TableTableAdapter.Fill(this.nexusDBDataSet.Contacts_Table);
            _currentGlobalBindingSource = contacts_TableBindingSource;
            _currentGlobalTable = "Contacts_Table";
        }

        // CallRegister_Table
        else if (SelectedTab == "mainTab2")
        {...some more code here...

I am using the code to dynamically update a dataset,datagridview and a bindingNavigator . The problem is i don't know how to tell if the tab is "Suppose to show".I don't know if it is a Parent or a child tabControle.

    private void MaintabControler_SelectedIndexChanged(object sender, EventArgs e)
    {
        SelectedTab = tabControl1.SelectedTab.Name.ToString();
        if (SelectedTab == "mainTab1") \\ This is true, and its the tab that is "suppose" to show

        SelectedTab = tabControl2.SelectedTab.Name.ToString();
        if (SelectedTab == "subTab1") \\ This is true too so the incorrect data gets loaded to the datagrid
    }

i would like to keep this one event and not revert to the 3 events from before.mainly because i can call the event when the form loads.but also keep all this under one aria and call a easy refresh event.

Upvotes: 0

Views: 1632

Answers (2)

man_luck
man_luck

Reputation: 1656

You can also check the selected tab like this:

    private void MaintabControler_SelectedIndexChanged(object sender, EventArgs e)
    {
if (tab1.SelectedTab == tab1.TabPages["mainTab1"])
        {
            // your stuff
        }
}

Upvotes: 1

JoelC
JoelC

Reputation: 3754

If I understand what you're asking:

The object sender argument on your event handler will contain the tab control that fired the event. Cast that to a tab control to know whether you are in the parent or the child tab control event.

Something like this:

TabControl currentControl = (TabControl)sender;
if (currentControl.Equals(tabControl1)
{
    ...

Upvotes: 1

Related Questions