我正在使用以下代码来检测datagridview中的按键:
Private Sub DataGridView1_mt_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1_mt.EditingControlShowing
AddHandler e.Control.KeyDown, AddressOf cell_Keydown
End Sub
Private Sub cell_Keydown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Space And CheckBox3.Checked = True Then
e.Handled = True
InputSimulator.SimulateTextEntry("_")
End If
End Sub
基本上我需要用下划线替换每个空格。到目前为止,代码工作除了2个问题:
1)e.handled似乎不影响输出。在下划线前面总是有空格。如何防止它输入?
2)每次我更换单元格时都会添加一个新的处理程序,如果我正在编辑第五个单元格,则结果将是一个空格,后跟5个下划线。我怎么能避免这个?
由于
答案 0 :(得分:0)
解决这个问题:
Dim eventhandleradded As Boolean = False
Private Sub DataGridView1_mt_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1_mt.EditingControlShowing
If eventhandleradded = False Then
AddHandler e.Control.KeyDown, AddressOf cell_Keydown
eventhandleradded = True
End If
End Sub
Private Sub cell_Keydown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Space And CheckBox3.Checked = True Then
e.Handled = True
e.SuppressKeyPress = True
InputSimulator.SimulateTextEntry("_")
End If
End Sub