2008-12-08 16 views
2

tab鍵有沒有一種簡單的方法方式與Tab鍵相同的左右移動控制完全是一個形式?這包括在細胞周圍移動上一個DataGridView等使用的WinForms,你應該設置窗體的KeyPreview屬性爲true請回車鍵的行爲就像形式

,並在窗體的按鍵事件,你應該有

private void Form1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == 13) 
     GetNextControl(ActiveControl, true).Focus(); 
} 
+0

您只需要重寫`ProcessCmdKey`,請參閱[這裏是如何](http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-make -the進入琴鍵的行爲樣的標籤鍵-IN-A-數據網格,移動到最下cell.aspx)和[這裏爲什麼] [2] [2]:HTTP: //blogs.msdn.com/b/jfoscoding/archive/2005/01/24/359334.aspx – 2012-09-11 23:17:02

回答

2

句柄是用於在單元格之間移動的自己的選項卡事件,則必須創建自定義數據網格控件並覆蓋onKeyUp事件,如下所示:

Public Class MyCustomDataGrid 
    Inherits DataGridView 

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs) 
     If e.KeyCode = Keys.Enter Then 
      e.Handled = True 
      Me.ProcessTabKey(Keys.Tab) 
     Else 
      MyBase.OnKeyUp(e) 
     End If 
    End Sub 
End Class 

將處理試圖選項卡時輸入的Tab鍵鍵雖然數據網格單元, 如果您還需要處理表單上的標籤,你將不得不做Marioh說,但有一點變化。

Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs) 
     If e.KeyCode = Keys.Enter AndAlso Not ActiveControl.GetType() Is GetType(Class1) Then 
      e.Handled = True 
      Me.ProcessTabKey(Not e.Shift) 
     Else 
      MyBase.OnKeyUp(e) 
     End If 
    End Sub 

你只需要添加一個檢查類型的主動控制,否則表格將停止工作您的自定義數據網格製表符碼。

2

因爲在DataGridView

0
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{ 
Keys keyPressed = (Keys)msg.WParam.ToInt32(); 
switch (keyPressed) 
{ 
    case Keys.Enter: 
    case Keys.Tab: 
     Control ctrl = this.GetNextControl(this.ActiveControl, true); 
     while (ctrl is TextBox == false) 
     { 
      ctrl = this.GetNextControl(ctrl, true); 
     } 
     ctrl.Focus(); 
     return true; 
    default: 
     return base.ProcessCmdKey(ref msg, keyData); 
} 
}