2010-01-25 31 views
3

我在包含一些文本框和按鈕的窗體中有一個子面板。我嘗試設置這些控件的tabstop和tabindex屬性,以便用戶可以從一個控件切換到下一個控件。但由於某些原因,TabB不起作用,當我按下Tab鍵時,Curor停留在同一個焦點上。我正在使用.NET 3.5框架的C#。下面是我的代碼的樣子 -Windows窗體 - Tab鍵在子面板中不起作用

rightPanel.Controls.Clear(); 
     marketMessageLabel = new Label(); 
     marketMessageLabel.Location = new Point(0, 20);    
     marketMessageLabel.AutoSize = false; 
     marketMessageLabel.Size = new Size(rightPanel.Width, 42); 
     marketMessageLabel.BackColor = Color.White;    
     marketMessageLabel.Font = new System.Drawing.Font("Verdana", 8.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     rightPanel.Controls.Add(marketMessageLabel);       

     signinUserNameLabel = new Label(); 
     signinUserNameLabel.Location = new Point(0, 150); 
     signinUserNameLabel.Size = new Size(60, 14); 
     signinUserNameLabel.BackColor = Color.White; 
     signinUserNameLabel.Text = "User Name";    
     signinUserNameLabel.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     rightPanel.Controls.Add(signinUserNameLabel); 

     signinUserNameTextBox = new TextBox(); 
     signinUserNameTextBox.Location = new Point(0, 170); 
     signinUserNameTextBox.Width = this.Width - 80; 
     signinUserNameTextBox.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));   
     signinUserNameTextBox.TabIndex = 0; 
     signinUserNameTextBox.TabStop = true; 

     rightPanel.Controls.Add(signinUserNameTextBox); 

     signinPasswordLabel = new Label(); 
     signinPasswordLabel.Location = new Point(0, 192); 
     signinPasswordLabel.Size = new Size(100, 14); 
     signinPasswordLabel.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     signinPasswordLabel.BackColor = Color.White; 
     signinPasswordLabel.Text = "Password";    
     rightPanel.Controls.Add(signinPasswordLabel);      

     signinPasswordTextBox = new TextBox(); 
     signinPasswordTextBox.Location = new Point(0, 210); 
     signinPasswordTextBox.Width = this.Width - 80;    
     signinPasswordTextBox.PasswordChar = '*'; 
     signinPasswordTextBox.TabIndex = 1; 
     signinPasswordTextBox.TabStop = true; 
     rightPanel.Controls.Add(signinPasswordTextBox); 

     signInButton = new Button(); 
     signInButton.Text = "Sign In"; 
     signInButton.Font = new System.Drawing.Font("Verdana", 9.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     signInButton.Width = 70;    
     signInButton.BackColor = Color.White; 
     signInButton.Location = new Point(0,240); 
     signInButton.Click += new EventHandler(signInButton_Click); 
     signInButton.TabIndex = 2; 
     signInButton.TabStop = true; 
     rightPanel.Controls.Add(signInButton); 
+0

改爲使用UserControl。並且不要在子控件上調用Dispose()時使用Clear(),它會永久性地泄漏控件窗口句柄。 – 2010-01-26 11:16:19

回答

0

請確保您爲標籤設置tabindex,儘管它不可​​聚焦。

從VS設計窗口,在設計中更多的在屏幕上表單,點擊

  • 查看菜單
  • Tab順序菜單選項

指向和點擊來設置順序的控件(包括標籤)。

希望這會有所幫助, 最好的問候, 湯姆。

1

解決方案是在面板上設置TabStop = true。

我只是跑了一個小測試,看起來winforms不會在tab面板之外的任何其他可調焦控件的情況下被選中。

實際上,您不會真正結束標籤到「面板上」,但它會讓您看到您看到的這個問題,並將其標籤爲第一個子控件。

5

另一個可能的問題是,如果「tab」不起作用的窗體位於未以模態方式顯示的窗體上。

由於某些原因,如果子窗體與.show一起顯示,並且您寧願顯示.ShowDialog表單,「tab」有時不起作用。

+0

'.ShowDialog'適合我。 – 2016-10-27 00:43:47

+0

我發現,如果在form.Show()之後調用Application.Run(),TabB將會起作用。我認爲這與窗口消息傳遞循環沒有通過form.show()自動實現有關。 – Dowlers 2017-09-14 05:07:57

1

如果窗體是無模式(與.Show顯示()),那麼你需要添加以下代碼以處理KeyDown事件:

private void YourForm_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Tab) 
     { 
      if (e.Modifiers == Keys.Shift) 
       this.ProcessTabKey(false); 
      else 
       this.ProcessTabKey(true); 
     } 
    } 

您還需要將KeyPreview屬性設置爲True。

相關問題