本文實例講述了WinForm中變Enter鍵為Tab鍵實現焦點轉移的方法,在進行C#應用程序開發時有一定的實用價值。分享給大家供大家參考。
具體實現代碼如下:
/// <summary>/// 窗體控件控制相關的方法/// </summary>public class ControlTools{ private Form frm; public ControlTools(Form frm) { this.frm = frm; } /// <summary> /// 窗體上所有子控件的回車設成Tab /// </summary> public void EnterToTab() { frm.KeyPreview = true; frm.KeyPress += new KeyPressEventHandler(frm_KeyPress); } /// <summary> /// 注冊窗體的KeyPress事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frm_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { frm.SelectNextControl(frm.ActiveControl, true, true, true, true); } } /// <summary> /// 把某一個控件的所有子控件(TextBox ComboBox)的回車設成Tab /// </summary> /// <param name="groupControl">容器控件</param> public void EnterToTab(Control groupControl) { foreach (Control control in groupControl.Controls) { if (control is TextBox || control is ComboBox) control.KeyPress += new KeyPressEventHandler(control_KeyPress); } } /// <summary> /// 注冊控件的KeyPress事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void control_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { SendKeys.Send("{Tab}"); e.Handled = false; } }}
希望本文所述變Enter鍵為Tab鍵的方法對大家C#程序設計有所幫助。
新聞熱點
疑難解答