2011-09-27 70 views
1

C#.Net fw 3.5,在WinForform中TabControl, 當用戶標籤頁上的最後一個控件出來時,那麼焦點應該移動到下一頁,並將焦點放在第一個控件中頁面,我該怎麼做?AutoTab到TabControl中的下一個TabPage

這是必要的我,因爲,在主入口的形式,存在被擺放出來的tabcontrol的側一些強制性的問題,以及一些控制是沒有必要所有的TabControl,

如果用戶訪問的每個控制然後焦點應該自動移動到下一頁,如果用戶只想填充附加信息,那麼他可以通過點擊保存按鈕來提交。

對此有任何建議。

+0

這是很難通過一種通用的方式覆蓋Tab鍵的行爲做。一個愚蠢的竅門工作:添加一個大小爲(0,0)的按鈕。在Enter事件中,將焦點設置爲要選擇的下一個控件。 –

回答

0

你的問題是不準確的 「C#.Net fw 3.5,在WinForform的TabControl中,當用戶選項卡上出現最後一個TabPage控件時,那麼焦點應該移動到下一頁,並且將第一個控件的焦點那個頁面?「

這是一個陳述或問題。我不明白。你需要什麼目標? 如果您希望用戶通過按Tab鍵訪問隨後的選項卡內的控件,則可以通過選項卡控件中的按下按鈕來執行此操作。在按鍵事件中,您可以以編程方式更改選項卡。 希望它有幫助。

代碼應該是這樣的。 生成tabcontrol的按鍵事件並監視TAB鍵的按下。

private void tabControl1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if(e.ToString().Equals("TAB") // I dont know what tab key returns. But is hould be something like this 
     { 
       tabControl1.SelectedTab = tabControl1.TabPages[1] ; 
       // now tabpage 2 has the focus 
       // You can also focus any control you want in here as follows: 
       tabControl1.TabPages[1].Control["control key"].Focus(); 
     } 
    } 

希望它足夠清晰的

+0

當在tabpage上的最後一個控件上按Tab鍵然後焦點移動到Tabcontrol的下一個控件時,我的tabindexes被設置好,並且光標不會聚焦到下一個tabpage。 如果您有任何解決方案,請提供並簡要說明。 –

+0

你想要什麼結果? –

+0

我有一個tabcontrol存在三個tabpages,當用戶按tab鍵最後控制tabpage然後下一個tabpage的第一個控件應該集中。 –

1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.ComponentModel; 

namespace CSBSSWControls 
{ 
    // Class inhertis TabControl 
    public class bssTabControl : TabControl 
    { 
     private bool AutoTab_; 
     [DefaultValue(false)] 
     public bool AutoTab { get { return AutoTab_; } set { AutoTab_ = value; } } 
     protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
     { 
      //property which determines auto change tabpages 
      if (AutoTab) 
      { 
       switch (keyData) 
       { 
        case Keys.Tab | Keys.Shift: 
         { 
          return SetNextTab(false); 
         } 
        case Keys.Tab: 
         { 
          return SetNextTab(true); 
         } 
       } 
      } 
      return base.ProcessCmdKey(ref msg, keyData); 
     } 
     private bool SetNextTab(bool Forward) 
     { 
      // getting cuurent active control 
      ContainerControl CC = this.FindForm(); 
      Control ActC = null; 
      while (CC != null) 
      { 
       ActC = CC.ActiveControl; 
       CC = ActC as ContainerControl; 
      } 
      //checking, current control should not be tabcontrol or tabpage 
      if (ActC != null && !(ActC is TabPage) && !(ActC is bssTabControl)) 
      { 
       //getting current controls next control if it is tab page then current control is surely that last control on that tab page 
       //if shift+tab pressed then checked its previous control, if it is tab page then current control is first control on the current tab page. 
       TabPage NC = ActC.FindForm().GetNextControl(ActC, Forward) as TabPage; 
       if (NC != null) 
        if (this.TabPages.Contains(NC)) 
         if (Forward) 
         { 
          //selecting next tab page 
          this.SelectedTab = NC; 
          return true; 
         } 
         else 
         { 
          if (this.TabPages.IndexOf(NC) > 0) 
          { 
           //selecting pervious tab page 
           this.SelectedIndex = this.TabPages.IndexOf(NC) - 1; 
           return true; 
          } 
         } 
      } 
      return false; 
     } 
    } 
}