Reputation: 797
I am using following code to detect keypresses in a 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
basically I need to replace every space with an underscore. The code is working so far except for 2 problems:
1) e.handled does not seem to affect the output. There is Always a space In front of the underscore. how can I prevent it from typing it?
2) Each time i change cell a new handler is added, and if I am editing for example the fifth cell the result will be a space followed by 5 underscores. How can I avoid this?
Thanks
Upvotes: 0
Views: 6041
Reputation: 797
Solved this way:
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
Upvotes: 1