2010-05-27 51 views
19

我正在用C#構建一個Windows Forms應用程序。當選項卡上的某個選項卡被選中時,如何觸發代碼?Windows窗體事件「在選擇選項卡」?

+1

你的意思是Tab Menu,TabControl,不是嗎? – 2010-05-27 13:41:20

+0

'tabControl.TabPages [tabControl.SelectedIndex]'給你在那個索引處的'TabPage'。 – 2016-08-30 01:23:58

回答

30

我認爲這是TabControl.SelectedIndexChanged事件。

只要看看MSDN。我從那裏拿走它。假設你命名了你的選項卡控件tabControl1。 您需要申請使用此事件:

tabContrl1.TabControl.SelectedIndexChanged += tabControl1_SelectedIndexChanged; 

並添加事件處理

private void tabControl1_SelectedIndexChanged(Object sender, EventArgs e) { 

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

你可以給我一個示例程序中使用的這行代碼的例子嗎?我只是不知道如何處理你提供的代碼,謝謝! – sooprise 2010-05-27 13:38:26

1

檢查這個helps你。 「SelectedIndexChanged」可能會幫助你。從MSDN

詳情here

13

TabControl及其SelectedIndexChanged事件會做你的需要。

例如,您在客戶檔案中有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; 
     } 
    } 
} 

下也很有用,同時與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()。

使用TabControl.TabPageCollection Members

EDIT#1

用於選擇特定選項卡,它只能通過0,1,2,而不是標籤名稱標識?

是的,您可能會增加或減少TabControl.SelectedIndex屬性以使特定的TabPage被選中/激活。

有一件事,確保你沒有在TabPages.Count - 1之外索引TabPage,因爲起始索引是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; 

注:

要使用我們的例子中,我們有兩個頁面,地址信息和交易繼續TabControl.SelectedIndex屬性的任何變化將觸發TabControl.SelectedIndexChanged事件。

+0

對於選擇特定的選項卡,它只能由0,1,2標識,而不是標籤名稱? – sooprise 2010-05-27 14:30:30

+0

請參閱我的編輯,以獲得對此已評論問題的回答。 =) – 2010-05-27 15:01:48

4

對於選擇一個特定的選項卡,它只能標識0,1,2,而不是標籤名稱?

您可以通過將事件偵聽器添加到實際選項卡而不是選項卡控件來實現此目的。

如果您有一個名爲tabHistory的選項卡,您可以在設計器中添加以下行。

this.tabHistory.Enter += new System.EventHandler(this.tabHistory_Enter); 

然後只需添加您的方法來捕捉事件。

private void tabHistory_Enter(object sender, EventArgs e) 
{ 
    MessageBox.Show("Hey! Ive got focus"); 
} 
+0

它只在單擊選項卡表單時才起作用。這是點擊兩次點擊更改! – Bytemain 2016-11-19 11:58:07

相關問題