确保表单具有此属性集:
Me.KeyPreview = True
由于您试图从键盘事件中指定第 1 列,因此您可以传递自己的事件参数:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If ((Control.ModifierKeys And e.Alt) AndAlso e.KeyCode = Keys.S) Then
Dim mea As New MouseEventArgs(MouseButtons.None, -1, -1, -1, -1)
Dim dgvcme As New DataGridViewCellMouseEventArgs(1, -1, -1, -1, mea)
DataGridView1_ColumnHeaderMouseClick(DataGridView1, dgvcme)
End If
End Sub
该事件现在应该具有e.ColumnIndex
属性集。
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If e.ColumnIndex = 1 Then
MessageBox.Show("OK")
End If
End Sub
或者,您可以只从两个地方调用一个子例程:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If ((Control.ModifierKeys And e.Alt) AndAlso e.KeyCode = Keys.S) Then
Call HandelColumnOneClick()
End If
End Sub
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If e.ColumnIndex = 1 Then
Call HandleColumnOneClick()
End If
End Sub
Private Sub HandleColumnOneClick()
'// Do Something
End Sub