2010-01-24 14 views
1

我有一個tabControl1在我的窗體中有三個TabPages,名爲TabPage1,TabPage2和TabPage3。C#如何在tabcontrol特定選項卡中觸發關鍵事件?

當TabPage 2有焦點時,我需要引發一個關鍵事件(導航箭頭鍵)。 此事件不應在其他TabPages中引發。

任何人都知道如何?

+0

老實說,你想從設計的角度來看,似乎有點怪我。如果你描述你的真實任務,這可能會更好嗎? – nightcoder 2010-01-24 20:42:44

+0

你的問題很不清楚。當你說'提高'時,你的意思是'處理'? – SLaks 2010-01-24 20:42:54

+0

對不起,不清楚。當TabPage2有焦點時,我需要做些事情。當我按下Arrow_Up時,它會改變TabPage2中的一些標籤。但是這隻會在TabPage2中掛起,而不會在其他標籤頁中。 – Qrew 2010-01-24 20:47:26

回答

1
protected override bool ProcessCmdKey(ref Message m, Keys keyData) 
     { 
      bool blnProcess = false; 
      if (keyData == Keys.Left) 
      { 
       blnProcess = true; 
       MessageBox.Show("Key left"); 
       if (myTabControl1.SelectedIndex == 1) 
        MessageBox.Show("inside"); 


      } 
     } 

此代碼似乎工作 所以,當我選擇了tabPage2一個消息告訴我的「內部」當我按下左箭頭鍵。

Probalby不正確的事情做的事情,但ATLEAST它的工作原理現在...

+0

這很好用!我的代碼爲一個非常類似的目的(檢測輸入鍵)是這樣的(在標籤控件的keydown事件中:if(tc1.SelectedIndex == 2 && e.KeyValue == 13)GetPersonData(); – Rob 2012-11-06 14:17:36

3

在選定的事件處理程序中,您可以將發件人轉換爲適當的控件並檢查其名稱。如果事件是從TabPage2生成的,則可以觸發該關鍵事件。

像這樣的事情

private void TabPage_Selected(object sender, EventArgs e) 
{ 
    TabPage source = sender as TabPage; 
    if(source.Name.equals("TabPage2")) 
    //Do whatever... 
} 
+0

private void tabControl1_KeyDown(object sender,KeyEventArgs e) MessageBox.Show(「Key pressed」); } 這應該給我一個消息框,當一個鍵被按下,當這個tabcontrol有焦點? Mamoo:您的代碼因爲某種原因不起作用 – Qrew 2010-01-24 21:04:57

+0

可能事件處理程序名稱和/或TabPage控件名稱不相同。無論如何,事情有點不清楚後,此評論... 預期的行爲是:顯示一條消息(或做別的...),如果按下一個鍵和焦點在某個TabPage上? – mamoo 2010-01-24 21:31:08

+0

我認爲問題是標籤頁不能有焦點(頁面上沒有任何對象可以選擇,只是一堆標籤)。我們必須檢查是否選擇了正確的選項卡,然後按下按鍵時的劑量 – Qrew 2010-01-24 21:41:17

1

你需要得到從TabControl的自己的控制,這樣就可以攔截箭頭鍵和生成的事件。爲您的項目添加一個新類並粘貼下面顯示的代碼。編譯。將新的控件從工具箱的頂部拖放到表單上。

using System; 
using System.Windows.Forms; 

class MyTabControl : TabControl { 
    public event EventHandler<KeyEventArgs> ArrowKeys; 

    protected void OnArrowKeys(KeyEventArgs e) { 
    EventHandler<KeyEventArgs> handler = ArrowKeys; 
    if (handler != null) handler(this, e); 
    } 
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { 
    if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right) { 
     var e = new KeyEventArgs(keyData); 
     OnArrowKeys(e); 
     if (e.Handled) return true; 
    } 
    return base.ProcessCmdKey(ref msg, keyData); 
    } 
} 

使用示例的形式:

private void myTabControl1_ArrowKeys(object sender, KeyEventArgs e) { 
    if (myTabControl1.SelectedIndex == 1) { 
    // Do something with e.KeyData 
    //... 
    e.Handled = true; 
    } 
} 
+0

我無法管理它工作。當按左/右箭頭時,它只是在標籤之間切換。 – Qrew 2010-01-24 21:17:53

+0

重點在哪裏?它不在標籤頁內的控件上嗎? – 2010-01-24 21:33:43

+0

標籤頁只有標籤。我認爲這是問題。我想用F2 + F3 + F4鍵導航到不同的標籤,然後當選擇標籤頁2時,我需要照顧箭頭鍵,因爲不同的標籤是數字,並且應該改變顏色以查看哪個標籤被「選擇」 (沒有選擇,但只是視覺上)。標籤是一堆數字,我需要用pageup和pagedown鍵來更改單個標籤(數字)。 – Qrew 2010-01-24 21:38:35

0

我這樣做在VB .NET中,我可以將它張貼在C#中,如果你真的需要它,但是這應該告訴你如何處理的捕事件。

Public Class Form1 

    'give a variable as a TabPage here so we know which one is selected(in focus) 
    Dim selectedPage As TabPage = TabPage1 

    'If a key is pressed when the tab control has focus, it checks to see if it is the right tab page 
    'and then show a message box(for demonstration) 
    Private Sub TabControl1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown 
     'The IF Not is to basically catch any odd happening that might occur if a key stroke gets passed with 
     'no selected tab page 
     If Not selectedPage Is Nothing Then 
      'If the tabpage is TabPage2 
      If selectedPage.Name = "TabPage2" Then 
       MessageBox.Show("Key Pressed") 
      End If 
     End If 
    End Sub 

    'This handles the actual chaning of the tab pages 
    Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected 
     selectedPage = TabControl1.SelectedTab 
    End Sub 

而現在你只需要使用實際的按鍵。

邁克爾Sarchet

0
private void Main_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.C) // I used the C Key 
    { 
     if (tabControl.SelectedIndex == 0) // control is only in tab 1 (index 0) endabled 
     { 
      // Your Code 
     } 
    } 
} 
相關問題