简体   繁体   English

Windows窗体事件“在选择选项卡上”?

[英]Windows Forms event “On Select Tab”?

I'm building a Windows Forms application in C#. 我正在用C#构建一个Windows窗体应用程序。 How do I trigger code when a certain tab on a tab menu is selected? 如何选择选项卡菜单上的某个选项卡时,如何触发代码?

I think it is the TabControl.SelectedIndexChanged event. 我认为这是TabControl.SelectedIndexChanged事件。

Just look at MSDN. 只看MSDN。 I took it from there. 我从那里拿走了它。 Suppose you named your tab control tabControl1 . 假设您将选项卡控件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. TabControl及其SelectedIndexChanged事件将满足您的需求。

For example, you have a Customer file with a TabControl in its details portion of the form. 例如,您在表单的详细信息部分中有一个带有TabControl的Customer文件。 You want to load lazy-load this customer's transactions when the user clicks the Transactions TabPage . 当用户单击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 . 使用TabControl时,以下内容也很有用。

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

Using the TabControl.TabPageCollection Members . 使用TabControl.TabPageCollection Members

EDIT #1 编辑#1

For selecting a specific tab, it can only be identified by 0, 1, 2, and not the tab name? 要选择特定选项卡,它只能用0,1,2标识,而不能用标签名称标识?

Yes, you might as well increment or decrement the TabControl.SelectedIndex property to make a specific TabPage selected/active. 是的,您也可以增加或减少TabControl.SelectedIndex属性以使特定的TabPage选择/激活。

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. 但有一点,请确保您不要将TabPageTabPages.Count - 1索引,因为起始索引为0,否则您将获得抛出的IndexOutOfRangeException

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. 注意:对TabControl.SelectedIndex属性的任何更改都将触发TabControl.SelectedIndexChanged事件。

For selecting a specific tab, can it only be identified by 0, 1, 2, and not the tab name? 要选择特定选项卡,是否只能通过0,1,2标识,而不是选项卡名称?

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. 如果您有一个名为tabHistory的选项卡,则可以在设计器中添加以下行。

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... 如果您有3个标签...

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...

Check if this helps you. 检查这是否有所帮助 " SelectedIndexChanged " might help you. SelectedIndexChanged ”可能对您有所帮助。

Details from MSDN are here MSDN的详细信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM