Ita*_*aro 34
我认为这是TabControl.SelectedIndexChanged事件.
只看MSDN.我从那里拿走了它.假设您命名了选项卡控件tabControl1.您需要使用以下方式订阅此活动:
tabContrl1.TabControl.SelectedIndexChanged += tabControl1_SelectedIndexChanged;
Run Code Online (Sandbox Code Playgroud)
并添加事件处理程序
private void tabControl1_SelectedIndexChanged(Object sender, EventArgs e) {
MessageBox.Show("You are in the TabControl.SelectedIndexChanged event.");
}
Run Code Online (Sandbox Code Playgroud)
Wil*_*ler 14
在TabControl其SelectedIndexChanged活动会做你的需要.
例如,您有一个Customer文件,其中包含TabControl表单的详细信息部分.您希望在用户单击"事务"时加载延迟加载此客户的事务TabPage.您的代码应该看起来像这个伪代码:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下在玩游戏时也很有用TabControl.
使用TabControl.TabPageCollection Members.
编辑#1
要选择特定选项卡,它只能用0,1,2标识,而不能用标签名称标识?
是的,您也可以增加或减少TabControl.SelectedIndex属性以使特定的TabPage选定/活动.
但有一两件事,要确保你没有索引TabPage出来的TabPages.Count - 1,因为一开始指数是0,否则你会得到一个IndexOutOfRangeException异常.
要继续我们的示例,我们有两个页面,地址信息和交易:
// 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;
Run Code Online (Sandbox Code Playgroud)
注意:对TabControl.SelectedIndex属性的任何更改都将触发该TabControl.SelectedIndexChanged事件.
小智 5
选择特定选项卡时,是否只能用0、1、2来标识,而不用选项卡名称来标识?
您可以通过将事件侦听器添加到实际选项卡而不是选项卡控件来完成此操作。
如果您有一个名为 tabHistory 的选项卡,则可以在设计器中添加以下行。
this.tabHistory.Enter += new System.EventHandler(this.tabHistory_Enter);
Run Code Online (Sandbox Code Playgroud)
然后只需添加您的方法来捕获事件。
private void tabHistory_Enter(object sender, EventArgs e)
{
MessageBox.Show("Hey! Ive got focus");
}
Run Code Online (Sandbox Code Playgroud)