Rob
Rob

Reputation: 1539

TabPage Click Events

I'm trying to automatically trigger events based on the tab page that is clicked on the tab control.

In design mode of my form, when I click on the tabs the properties box says Tabs System.Windows.Forms.TabControl whichever tab is selected. However, I have to click on the actual page, not the tab for the property to change to the name of the pages e.g. TaskListPage System.Windows.Forms.TabPage.

My tabcontrol is called Tabs and I was trying to test it out using the code below which is supposed to display a message based on the tab option.

 private void Tabs_SelectedIndexChanged(object sender, EventArgs e)

        {
            if (Tabs.SelectedTab == TaskListPage)
            {
                MessageBox.Show("TASK LIST PAGE");
            }
            else if (Tabs.SelectedTab == SchedulePage)
            {
                MessageBox.Show("SCHEDULE PAGE");
            }
        }

When I test the code above, nothing is happening.

Any help in getting the events working when a specific tab is clicked would be greatly appreciated!

Thankyou

Upvotes: 17

Views: 60399

Answers (3)

digisoft
digisoft

Reputation: 5

private void tabControl1_Click(object sender, EventArgs e)
{
    if (tabControl1.SelectedTab.Text =="All")
    {
        MessageBox.Show("All");
    }
    else
    {
        MessageBox.Show("hello world");
    }
}

Try This It should be work

Upvotes: 0

Larryrl
Larryrl

Reputation: 131

This first part goes in the

    public Form1()
    {
// This is near the top of the form 1 code in form1.cs

        InitializeComponent();
        tabControl1.SelectedIndexChanged += new EventHandler(TabControl1_SelectedIndexChanged);
    }

Then down below in your regular code you simply tell which control should have the focus after the tab page is clicked. Which in my word processor, I used a rich text box, and tab controls to simulate the msword ribbon. The rich text control in my case is not on a tab page as my tab pages cover maybe 1 or 2 inch on the top of the form

private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {

        richTextBox1.Focus();
    }

This is what I call my word processor. It's here for anyone that would like to use it. Larry's Journal

Upvotes: 1

LarsTech
LarsTech

Reputation: 81675

It sounds like you don't have it wired up:

public Form1() {
  InitializeComponent();    
  Tabs.SelectedIndexChanged += new EventHandler(Tabs_SelectedIndexChanged);
}

There are other events that can give you this information as well: Selected and Selecting.

void Tabs_Selected(object sender, TabControlEventArgs e) {
  if (e.TabPage == TaskListPage) {
    // etc
  }
}

Upvotes: 23

Related Questions