我正在用C#構建一個Windows Forms應用程序。當選項卡上的某個選項卡被選中時,如何觸發代碼?Windows窗體事件「在選擇選項卡」?
回答
我認爲這是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.");
}
你可以給我一個示例程序中使用的這行代碼的例子嗎?我只是不知道如何處理你提供的代碼,謝謝! – sooprise 2010-05-27 13:38:26
的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
玩。
- TabControl.TabPages.Add();
- TabControl.TabPages.Contains();
- TabControl.TabPages.ContainsKey();
- TabControl.TabPages.Insert();
- TabControl.TabPages.Remove();
- TabControl.TabPages.RemoveAt();
- 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,1,2標識,而不是標籤名稱? – sooprise 2010-05-27 14:30:30
請參閱我的編輯,以獲得對此已評論問題的回答。 =) – 2010-05-27 15:01:48
對於選擇一個特定的選項卡,它只能標識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");
}
它只在單擊選項卡表單時才起作用。這是點擊兩次點擊更改! – Bytemain 2016-11-19 11:58:07
- 1. 實現Windows窗體C++選項卡選定事件
- 2. 選擇窗體初始化選項卡
- 3. JavaFx:選項卡上的事件篩選器(選項卡窗格)
- 4. Windows窗體選項卡切換
- 5. JQuery選項卡,選項卡上選擇觸發事件
- 6. jQuery - 陷印選項卡選擇事件
- 7. Excel Ribbon選項卡選擇Chang事件
- 8. C#WinForms:在新窗體加載選擇選項卡可見性
- 9. 在窗體上移動自定義選項卡/子選項卡
- 10. jQuery UI選項卡,選擇/取消選擇(摺疊)事件
- 11. 窗口選項卡按下事件
- 12. Windows窗體中的自定義選項卡控件
- 13. 如何將許多控件添加到Windows窗體選項卡?
- 14. 窗體窗體選項卡式頁面:從命令行在不同選項卡上啓動窗體
- 15. 窗體選擇選項需要
- 16. 窗體選擇選項斷線重疊
- 17. 彈簧窗體選項默認選擇
- 18. zf2窗體禁用選擇選項
- 19. 如何強制選擇特定的jquery-ui選項卡索引或強制在特定選項卡上選擇「選項卡選擇」事件?
- 20. Outlook 2010加載項綁定事件選項卡選擇
- 21. 在Windows窗體中啓用或禁用選項卡
- 22. 在Windows窗體中舍入選項卡標題
- 23. 如何在C#Windows窗體中創建框架/選項卡?
- 24. jQuery Mobile選項卡 - 選項卡選定的事件
- 25. 彈簧窗體選項不選擇項目,選項是
- 26. EXTJS OnClick選項卡事件
- 27. Chrome新選項卡事件
- 28. 選項卡關閉事件
- 29. 窗體之間有什麼區別:選擇和窗體:選項
- 30. Ajax選項卡控件中的選項卡更改事件
你的意思是Tab Menu,TabControl,不是嗎? – 2010-05-27 13:41:20
'tabControl.TabPages [tabControl.SelectedIndex]'給你在那個索引處的'TabPage'。 – 2016-08-30 01:23:58