2012-08-10 26 views
5

Visual Studio 2010中,C#如何啓用一個WinForm按鈕,在時間Tab鍵

接收焦點我有一個ComboBoxDropDownAutoComplete設置爲SuggestAppendAutoCompleteSourceListItems。用戶將數據輸入到數據中,直到輸入正確的數據。如果數據與其中一個列表項相匹配,則禁用組合框旁邊的按鈕。

如果用戶點擊標籤鍵,自動完成功能會接受當前的建議。它也會轉到啓用的選項卡序列中的下一個控件。當然,因爲我想讓它進入disbabled按鈕,我需要啓用它,只要我驗證條目。

問題是沒有我嘗試過的事件,PreviewKeyDown,LostFocus,SelectedIndexChanged允許我啓用該按鈕及時處理它並獲得焦點。它總是會以總是啓用的標籤順序進入下一個按鈕。

我準備離開啓用按鈕,並且如果過早按下它會給出錯誤,但我不想這樣做。我也不想進入特殊模式標誌來跟蹤這些控件何時獲得焦點。驗證似乎是正常的事情,但我卡住了。

如果當用戶進行匹配時SelectedIndexChanged工作,這將很容易。當盒子清除時,或者當找到類型匹配時,它不會被觸發。

+0

使用TextChanged事件。 – 2012-08-10 15:16:32

回答

0
try this : 

KEY_PRESS事件:

if (e.KeyData == Keys.Enter) 
     { 
      button2.Enabled = true; 
      button2.Focus(); 
     } 
+0

這不會對Tab鍵起作用。 KeyPress事件也不提供KeyData。 – 2012-08-10 13:34:13

0

相反,你提到的事件hanlders,(引發LostFocus,的SelectedIndexChanged和PreviewKeyDown)的使用組合框的 「驗證」 的事件來設置按鈕的啓用狀態。

您可能還需要手動對焦按鈕以強制焦點移動到它。

例如

private void comboBox1_Validated(object sender, EventArgs e) 
    { 
     button1.Enabled = true; 
     button1.Focus(); 
    } 
+1

在驗證中分配焦點假定太多。如果用戶點擊表單上的另一個按鈕,該怎麼辦呢?它也在幫助中被警告。看到我的評論ahazzah。 – 2012-08-10 14:25:48

+0

這是一個公平的電話。我誤解了你的意思是「我希望它進入禁用按鈕」。重新閱讀後,很明顯你的意思是,如果用戶點擊製表鍵,你就需要這種行爲。 – 2012-08-12 08:45:26

1

您可以創建自己的ComboBox類來封裝此行爲。事情是這樣的:

using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      this.myComboBox1.TheButton = this.button1; 

      this.myComboBox1.Items.AddRange(new string[] { 
       "Monday", 
       "Tuesday", 
       "Wednesday", 
       "Thursday", 
       "Friday", 
       "Saturday", 
       "Sunday" 
      }); 

      button1.Enabled = false; 
     } 
    } 

    public class MyComboBox : ComboBox 
    { 
     public Control TheButton { get; set; } 

     public MyComboBox() 
     { 
     } 

     bool IsValidItemSelected 
     { 
      get { return null != this.SelectedItem; } 
     } 

     protected override void OnValidated(EventArgs e) 
     { 
      if (null != TheButton) 
      { 
       TheButton.Enabled = this.IsValidItemSelected; 
       TheButton.Focus(); 
      } 

      base.OnValidated(e); 
     } 

     protected override void OnTextChanged(EventArgs e) 
     { 
      if (null != TheButton) 
      { 
       TheButton.Enabled = this.IsValidItemSelected; 
      } 

      base.OnTextChanged(e); 
     } 
    } 
} 
+0

感謝您抽出寶貴時間來創建此文件。問題在於按鈕焦點正在OnValidated事件中設置。 VS2010幫助指出:「不要嘗試在Enter,GotFocus,Leave,LostFocus,Validating或Validated事件處理程序中設置焦點,否則可能導致應用程序或操作系統停止響應。」其次驗證事件發生在離開事件之後,這也太遲了,所以它看起來並不像我想要的那樣能夠做到我本來想要的。 – 2012-08-10 12:41:07

+0

「所以看起來我不能做我想做的事。」 我在答案中創建的測試應用程序做你想做的。你試過了嗎? – ahazzah 2012-08-10 15:18:10

+0

不,我沒有,因爲謹慎似乎非常具體。它確實有效。多用一點tweeking它將啓用按鈕,只要用戶輸入完成。我確實有另一種解決方案,使用不違反警告的ProcessCmdKey表單重寫。我可能會過度直譯,但在使用Delphi進行Win32的最近15年工作後,我對.Net很陌生,並且存在足夠的分歧,我非常謹慎。 – 2012-08-10 18:51:27

0

有了一些思考其他答案在這裏,我想出了不使用自動完成工作的部分塞納里奧。副作用是PreviewKeyDown事件被第二次調用,因此驗證被調用兩次。我想知道爲什麼......也許我應該問另一個問題。

private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { 
     if (e.KeyData == Keys.Tab) { 
     if (ValidationRoutine()) { 
      e.IsInputKey = true; //If Validated, signals KeyDown to examine this key 
     } //Side effect - This event is called twice when IsInputKey is set to true 
     }   
    } 

    private void comboBox1_KeyDown(object sender, KeyEventArgs e) { 
     if (e.KeyData == Keys.Tab) { 
      e.SuppressKeyPress = true; //Stops further processing of the TAB key 
      btnEdit.Enabled = true; 
      btnEdit.Focus(); 
     } 
    } 

一旦你打開AutoCompleteMode任何比None設定,則KeyDown事件不火Tab了,關鍵是默默地吃掉。

相關問題