Crashie-J
Crashie-J

Reputation: 113

Focusing on object from another form

I have two forms which both have multiple tab pages with each tab having a button. I would like to know how to focus on the other page's respective tab when the button is clicked (ie. "button2" on "tabPage2" in "Form1" leads to and focuses on "tabPage2" on "Form2")

// this belongs on Form1
private void button2_Click(object sender, EventArgs e)
{
    Project1.NewFolder1.Form2 settingsForm = new Project1.NewFolder1.Form2();
    // what I put here to focus on tabPage2 on form 2?
    settingsForm.Show();
    this.Close();
}

how should I go about this in an easily digestible format?

Upvotes: 1

Views: 51

Answers (1)

Daniel B
Daniel B

Reputation: 3185

You can't access directly the tab control but you can add the following method to Form2 class

public void SetSelectedTab(int selectedTabIndex){
   tabControl1.SelectTab(selectedTabIndex);
}

and before showing it you can call it:

// focus on tabPage2 on form2
settingsForm.SetSelectedTab(1);  //staring from 0
settingsForm.Show();

Upvotes: 2

Related Questions