C#中方向鍵與斷行符號鍵切換控制項焦點的兩種方法

來源:互聯網
上載者:User
環境:介面上有TextBox,ComboBox等控制項。

不建議把左右方向鍵都用來切換焦點,否則你在TextBox裡面改變游標所在字元位置就不方便了。

方法一:笨方法,需為每個控制項單獨註冊事件處理

以TextBox為例,代碼如下:

 1 private void textbox_KeyDown(object sender, KeyEventArgs e)          2 {              3     if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Enter)              4     {                  5         e.SuppressKeyPress = true;                  6         System.Windows.Forms.SendKeys.Send("{Tab}");              7     }              8     else if (e.KeyCode == Keys.Up)              9     {                 10         e.SuppressKeyPress = true;                 11         System.Windows.Forms.SendKeys.Send("+{Tab}");             12     }         13 }

方法二:簡單方法,無需為每個控制項單獨註冊事件處理,僅需在表單類上加入如下代碼:

 1 //上、下方向鍵,及斷行符號鍵切換控制項焦點 2 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 3 { 4     Keys key = (keyData & Keys.KeyCode); 5     if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Enter)              6     {                                   7       System.Windows.Forms.SendKeys.Send("{Tab}");  8       return true;             9     }             10     else if (e.KeyCode == Keys.Up)             11     {                                  12       System.Windows.Forms.SendKeys.Send("+{Tab}");13       return true;             14     }   15     return base.ProcessCmdKey(ref msg, keyData);16 }

到此,切換控制項焦點的功能已實現,現在有個新的需求,表單介面上有兩個ComboBox控制項cmbMeas和cmbRemark,我希望在這兩個控制項上Enter斷行符號時提交,而不是切換焦點,那怎麼辦呢?那就需要判斷當前擁有焦點的控制項是不是cmbMeas或cmbRemark,上面的代碼需要稍微改動下,具體代碼如下:

 1 //API聲明:擷取當前焦點控制項控制代碼       2 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)] 3 internal static extern IntPtr GetFocus(); 4  5 //擷取當前擁有焦點的控制項 6 private Control GetFocusedControl() 7 { 8      Control focusedControl = null; 9      // To get hold of the focused control:10      IntPtr focusedHandle = GetFocus();11      if (focusedHandle != IntPtr.Zero)12          //focusedControl = Control.FromHandle(focusedHandle);13          focusedControl = Control.FromChildHandle(focusedHandle);14      return focusedControl ;15  }16 17 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)18 {19     Keys key = (keyData & Keys.KeyCode);20     Control ctrl = GetFocusedControl();21     if (e.KeyCode == Keys.Down || (key == Keys.Enter && ctrl.Name != "cmbMeas" && ctrl.Name != "cmbRemark"))             22     {                                  23         System.Windows.Forms.SendKeys.Send("{Tab}"); 24         return true;            25     }             26     else if (e.KeyCode == Keys.Up)             27     {                                  28         System.Windows.Forms.SendKeys.Send("+{Tab}");29         return true;             30     }   31     return base.ProcessCmdKey(ref msg, keyData);32 }

說明:

Control.FromHandle 方法

返回當前與指定控制代碼關聯的控制項;如果找不到帶有指定控制代碼的控制項,就返回Null 參考。

Control.FromChildHandle 方法

如果需要返回擁有多個控制代碼的控制項,應使用 FromChildHandle 方法。

此方法沿著視窗控制代碼父級鏈向上搜尋,直到找到與控制項關聯的控制代碼。此方法比 FromHandle 方法更可靠,因為它正確返回擁有多個控制代碼的控制項。

對於使用者自訂控制項,應當使用FromChildHandle 方法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.