[英]C# Textbox Focus
在Windows窗體C#應用程序中,我有兩個文本框控件。
我將tabindex 1設置為第一個控件我將tabindex 2設置為第二個控件
當我在第一個控件中按Tab鍵時,我需要執行一些檢查操作。
我嘗試使用此代碼攔截Tab鍵
control1__KeyPress(...)
{
if (e.KeyChar == (char)Keys.Tab)
{
....
}
}
但該事件未觸發。
我試圖做到這一點
control1__KeyDown(...)
{
}
但該事件也未觸發。
在第二個控件獲得焦點之前,如何截獲Tab鍵?
謝謝
正如我在評論中所說,您應該根據需要使用適當的事件處理程序。 如果您的意圖只是驗證用戶輸入,那么有一個Validating事件可能會有所幫助。 如果在控件失去焦點時要提醒您的意圖,則有一個離開事件。
如果您確實只想處理TAB密鑰,則應采用覆蓋ProcessCmdKey的方式,如本例所示
public class Form1 : System.Windows.Forms.Form
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData )
{
if( keyData == Keys.Tab)
{
// This method will be called for every control in your form
// so probably you need to add some checking on the Handle
// of the control that originates the call
if(msg.Hwnd == yourTextBox.Handle)
{
ExecuteYourTabProcessinLogicHere();
// Returning TRUE here halts the normal behavior of the
// TAB key, you could change this based on the result
// of your logic
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
但是,我應該警告您,對於希望該密鑰具有良好預定義行為的用戶,使用TAB密鑰可能會感到非常不安
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.