I'm building a Windows Forms application in C#. How do I trigger code when a certain tab on a tab menu is selected?
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.");
}
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
.
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.
For selecting a specific tab, can it only be identified by 0, 1, 2, and not the tab name?
You can do this by adding the event listener to the actual tab rather than to the tab control.
If you had a tab called tabHistory, you could add the following line in the designer.
this.tabHistory.Enter += new System.EventHandler(this.tabHistory_Enter);
Then just add your method to catch the event.
private void tabHistory_Enter(object sender, EventArgs e)
{
MessageBox.Show("Hey! Ive got focus");
}
if you for example have 3 tabs...
if (tabControl.SelectedTab == tabControl.TabPages[0])
do something...
if (tabControl.SelectedTab == tabControl.TabPages[1])
do something else...
if (tabControl.SelectedTab == tabControl.TabPages[2])
do something else...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With