如何覆盖datagridview中的输入键,以便将焦点设置到下一列而不是下一行?使用输入键覆盖datagridview导航
回答
您只需处理DataGridView的KeyDown事件并在处理程序中检查当前按下的按键是否为Enter键。如果是这样,只需设置DataGridView的下一个单元格的CurrentCell(也检查,如果这是该行的最后一个单元格,在这种情况下,移动到下一行的第一个单元格。)
Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Enter Then
Dim numCols As Integer = DataGridView1.ColumnCount
Dim numRows As Integer = DataGridView1.RowCount
Dim currCell As DataGridViewCell = DataGridView1.CurrentCell
If currCell.ColumnIndex = numCols - 1 Then
If currCell.RowIndex < numRows - 1 Then
DataGridView1.CurrentCell = DataGridView1.Item(0, currCell.RowIndex + 1)
End If
Else
DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex + 1, currCell.RowIndex)
End If
e.Handled = True
End If
End Sub
我找到了我需要的帮助,以便让我进入this link,具体在 张荣春对子类别DataGridView
的反馈意见覆盖ProcessDialogKey
。我在下面的例子中演变了他的工作以满足我自己的要求。
目标:严格地说,当按下ENTER键时,只有在焦点将任何单元格留在第二个单元格上的情况下,才能将焦点从网格最后一行末尾的单元格跳到下一行到最后一行。
背景:我已经启用了Grid选项,允许用户在数据库中创建新行,所以为了使它们变得简单,默认的Grid行为是一个新的空行总是被添加在底部,一旦用户甚至不慎移动到它上面,数据库中就会创建一个新的空行。用户通常在完成任何单元格中的编辑后轻按ENTER键,因此用户不必创建新的空白行即可将其编辑结束至底部的最后一个单元格。
请注意,在这种情况下,我只是将CurrentCell留在原来的位置,但可以在调用'return true'之前重新分配它。
public class DataGridViewAddRow : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
//cell is in Edit mode
if (keyData == Keys.Enter)
{
if (this.CurrentCell.RowIndex == this.Rows.Count-2)
{
return true;
}
}
return base.ProcessDialogKey(keyData);
}
}
DataGridView的焦点到下一个单元或列时,输入键被按下VB.net:
私人小组DataGridView1_CellEndEdit(BYVAL发件人为对象,BYVALË作为System.Windows.Forms.DataGridViewCellEventArgs)把手DataGridViewX1.CellEndEdit
如果DataGridView1.CurrentCell.ColumnIndex = DataGridView1.ColumnCount - 1然后 DataGridView1.CurrentCell = DataGridView1.Item(0,DataGridView1.CurrentCell.RowIndex + 1) 否则 SendKeys.Send(“{}向上“) SendKeys.Send( “{右}”) 结束如果
结束子
Private Sub DbGDetail_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DbGDetail.KeyDown
If e.KeyCode = Keys.Enter Then
SendKeys.Send("{Tab}")
e.Handled = True
End If
End Sub
Private Sub DbGDetail_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DbGDetail.CellEndEdit
SendKeys.Send("{up}")
SendKeys.Send("{Tab}")
End Sub
公共类CustomDataGridView 继承的DataGridView
<System.Security.Permissions.UIPermission(_
System.Security.Permissions.SecurityAction.LinkDemand, _
Window:=System.Security.Permissions.UIPermissionWindow.AllWindows)> _
Protected Overrides Function ProcessDialogKey(_
ByVal keyData As Keys) As Boolean
' Extract the key code from the key value.
Dim key As Keys = keyData And Keys.KeyCode
' Handle the ENTER key as if it were a RIGHT ARROW key.
If key = Keys.Enter Then
Return Me.ProcessTabKey(keyData)
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
<System.Security.Permissions.SecurityPermission(_
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Protected Overrides Function ProcessDataGridViewKey(_
ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
' Handle the ENTER key as if it were a RIGHT ARROW key.
If e.KeyCode = Keys.Enter Then
Return Me.ProcessTabKey(e.KeyData)
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function
末级
- 1. 在AngularJS输入字段中覆盖键导航
- 2. 无法覆盖的导航键UICollectionViewController
- 3. 覆盖preventDefault onclick当选项卡和输入用于导航
- 4. 通过输入键导航
- 5. 导航覆盖其他divs
- 6. 覆盖导航页面
- 7. Android的webview键盘覆盖输入
- 8. 使UIImage全屏覆盖导航和Tabbar
- 9. Windows窗体 - 覆盖使用其他键输入
- 10. DataGridView存储覆盖
- 11. 在datagridview中检测导航按键
- 12. Android - 键盘覆盖输入框 - 即使在Chrome浏览器中
- 13. 导航栏覆盖以下文本
- 14. 导航图形覆盖在视频上
- 15. 导航实验无法渲染覆盖
- 16. 移动导航覆盖问题
- 17. CSS导航栏覆盖bootstrap模式
- 18. 如何覆盖“默认”导航栏?
- 19. 带全屏覆盖的bootstrap4导航栏
- 20. 照片库导航 - css覆盖
- 21. Bootstrap footer导航覆盖PopUp Divs
- 22. 添加覆盖导航栏的UIImageView
- 23. 覆盖导航移动错误的PictureScrollField
- 24. 手写笔导航栏覆盖文字
- 25. Bootstrap导航栏覆盖缩放内容
- 26. 导入时覆盖常量
- 27. 删除/覆盖导入
- 28. 如何在Windows窗体中使用导航键作为输入
- 29. kendo grid - 使用箭头键导航并输入单元格
- 30. 在Datagridview和导航键中使用Keydown事件