views:

103

answers:

3

Hey SO,

I'm building a WinForm application in C# and would like to know how to trigger code when a certain tab on a tab menu is selected. Thanks!

+1  A: 

Check if this helps you. "SelectedIndexChanged" might help you.

Details from MSDN are here

Ram
+7  A: 

I think it is the TabControl.SelectedIndexChanged Event

Just look at MSDN i took it from there Suppose you named your tab control tabControl1 You need to subscribe to this event using

tabContrl1.TabControl.SelectedIndexChanged += tabControl1_SelectedIndexChanged;

and add the event handler

private void tabControl1_SelectedIndexChanged(Object sender, EventArgs e) {

   MessageBox.Show("You are in the TabControl.SelectedIndexChanged event.");

}
Itay
Can you give me an example of this line of code being used in a sample program? I just don't know what to do with the code you provided, thanks!
Soo
A: 

The TabControl and its SelectedIndexChanged event will do what you need.

For example, you have a Customer file with a TabControl in its details portion of the form. You want to load lazy-load this customer's transactions when the user clicks the Transactions TabPage. Your code should look like this pseudo-code:

public partial class CustomerMgmtForm : Form {
    // Assuming the design already done, so the TabControl control exists on your form.
    public CustomerMgmtForm() {
        tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
    }

    private void tabControl1_SelectedIndexchanged(object sender, EventArgs e) {
        switch((sender as TabControl).SelectedIndex) {
            case 0:
                // Do nothing here (let's suppose that TabPage index 0 is the address information which is already loaded.
                break;
            case 1:
                // Let's suppose TabPage index 1 is the one for the transactions.
                // Assuming you have put a DataGridView control so that the transactions can be listed.
                // currentCustomer.Id can be obtained through the CurrencyManager of your BindingSource object used to data bind your data to your Windows form controls.
                dataGridview1.DataSource = GetTransactions(currentCustomer.Id);
                break;
        }
    }
}

The following are also useful while playing with the TabControl.

  1. TabControl.TabPages.Add();
  2. TabControl.TabPages.Contains();
  3. TabControl.TabPages.ContainsKey();
  4. TabControl.TabPages.Insert();
  5. TabControl.TabPages.Remove();
  6. TabControl.TabPages.RemoveAt();
  7. TabControl.TabPages.RemoveByKey().

Using the TabControl.TabPageCollection Members.

EDIT #1

For selecting a specific tab, it can only be identified by 0, 1, 2, and not the tab name?

Yes, you might as well increment or decrement the TabControl.SelectedIndex property to make a specific TabPage selected/active.

One thing though, make sure you don't index a TabPage out of the TabPages.Count - 1, since the start index is 0, otherwise you'll get an IndexOutOfRangeException thrown.

To continue with our example where we have two pages, the Address information and the Transactions:

// Will automatically change the selected tab to the Transactions TabPage.
tabControl1.SelectedIndex = 1; 

// Considering there a count of two TabPages, the first is indexed at 0, and the second at 1.  
// Setting the SelectedIndex property to 2 will throw.
tabControl1.SelectedIndex = 2; 

Note: Any change to TabControl.SelectedIndex property will trigger the TabControl.SelectedIndexChanged event.

Will Marcouiller
For selecting a specific tab, it can only be identified by 0, 1, 2, and not the tab name?
Soo
Please see my edit for an answer to this commented question. =)
Will Marcouiller