0

I am developing an application in C# Windows Forms, and I would like to create an event handler/event handlers based on whether or not a particular tab page of a tab control is selected. So for example, if I have three tab pages:

tabPage1,
tabPage2,
tabPage3,

which belong to

tabControl1,

I need the code to either:

  1. Have three separate event handlers for each tab page
  2. Have one event handler and inside of the event handler there is code which can determine the tab page that is currently selected (e.g. a case statement of some sort)

I have looked at several examples thus far, but none seem to do what I need. How can I create this event/ these events?

1
  • you can also use entered event. Had pretty good success with it Commented Mar 11, 2012 at 20:49

5 Answers 5

4

May be something like this:

Make use of TabControl.Selected

private void tabControl1_Selected(Object sender, TabControlEventArgs e) 
{

   if(e.TabPage == tabPage1) 
     DoSomethingInRelationOfTab1();
   else if(e.TabPage == tabPage2)
     DoSomethingInRelationOfTab2();
   ....
   ....
}
Sign up to request clarification or add additional context in comments.

3 Comments

I encountered the following error when I tried this code 'App.Form1.tabPage1' is a 'field' but is used like a 'type' Instead on using if(e.TabPage is tabPage1) I tried if(e.TabPage == tabPage1) but it did not work
"(e.TabPage is tabPage1)" switch "e.TabPage" and "tabPage1"
@user1224504: I corrected the answer to ==. What is not working with that pattern?
1

Like this?

private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
    MessageBox.Show("Current Tab: " + tabControl1.SelectedTab.Text);
}

Comments

0

Another solution is to subclass TabPage

class MyTabPage : TabPage {
    event EventHandler Activated;

    public void OnActivated() {
        if (Activated != null)
           Activated(this, EventArgs.Empty);
    }
}

void HandleTabIndexChanged(object sender, EventArgs args) {
    var tabControl = sender as TabControl;
    var tabPage = tabControl.SelectedTab as MyTabPage;
    if (tabPage != null)
        tabPage.OnActivated();
}

Comments

0

You should look for the VisibleChanged event on a child control of the tab page. This event will be fired for all the child controls in the tab page.

This is very useful when you place a CustomControl on each page. Then you can update the CustomControl when VisibleChanged is fired.

Comments

0

This was helping me out:

private void tabControl1_Selected(Object sender, TabControlEventArgs e)
{
    // Could be initialized in "Form_Load"
    var validTabPages = new[]
    {
        tabPage1,
        tabPage2,
        tabPage3,
        tabPage4
    };

    // If not a valid TabPage, just return
    if (!validTabPages.Contains(e.TabPage))
        return;

    pictureBox2.Parent.Controls.Remove(pictureBox2);
    pictureBox5.Parent.Controls.Remove(pictureBox5);

    e.TabPage.Controls.Add(pictureBox2);
    e.TabPage.Controls.Add(pictureBox5);
}

6 Comments

you could combine this with @Tigran s example
I don't quite understand how this code works and hence I am not sure how to test it in my application (I am relatively new to C# programming). Can the code be commented a bit more if possible?
@Tigran @mo. This is what I tried but the pictureboxes did not move from one tab to the next when I clicked the tabpage. (I know it works because I tried the code on a button click event) ` private void tabControl1_Selected(Object sender, TabControlEventArgs e) { if(e.TabPage == tabPage1) { this.tabPage2.Controls.Add(this.pictureBox5); } else if (e.TabPage == tabPage2) { this.tabPage2.Controls.Add(this.pictureBox5); } }`
I'm sorry if I'm a bit slow to catch on but I'm still unsure of how to integrate your code into the application. Where does each of these functions go? This code looks very well thought out but it also looks very foreign to me :/. Please help if you can...any sort of explanation pointing me in the direction of how to setup this code would be appreciated.
okay you want to add a picturebox to the current selected tabpage?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.