是否有任何机构可以给我代码示例如何捕获datagridview cell keypress事件? Datagridview_keypress没有帮助。
由于
答案 0 :(得分:4)
根据Fco Navarro的回答,除了使用e.Control
并不总是有效,因为e
传递给EditingControlShowing
事件ByVal
意味着任何更改控件(例如,更改.Text
属性)不会反映在DataGridView中。如果您需要对事件处理程序中的实际TextBox控件执行任何操作,则可以使用DataGridView1.EditingControl
而不是e.Control
。
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
txtNumeric = CType(DataGridView1.EditingControl, DataGridViewTextBoxEditingControl)
End Sub
Private Sub txtNumeric_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNumeric.KeyPress
txtNumeric.Text = txtNumeric.Text.ToUpper()
End Sub
答案 1 :(得分:1)
试试这个:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing If Me.DataGridView1.CurrentCell.ColumnIndex = 0 And Not e.Control Is Nothing Then Dim tb As TextBox = CType(e.Control, TextBox) AddHandler tb.KeyDown, AddressOf TextBox_KeyDown AddHandler tb.KeyPress, AddressOf TextBox_KeyPress End If End Sub Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) If e.KeyCode = Keys.Space Then flag = True End If End Sub Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) e.Handled = flag flag = False End Sub
摘自here。
答案 2 :(得分:1)
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl)
End Sub
Private Sub txtNumeric_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumeric.KeyDown
If (e.KeyData >= Keys.A And e.KeyData <= Keys.Z) Then
e.SuppressKeyPress = True
End If
End Sub
答案 3 :(得分:0)
如果您在asp.net(网站)中使用gridview,则无法实现。没有按键事件。 确实可能,但你必须使用JavaScript在每个keychanged(客户端)上进行回发。但这不是一个很好的编程风格,不应该使用(你正在进行许多回调,这会使整个系统变慢)。
如果您使用Windows表单:请查看sysdragon的答案。
希望这有点帮助。
最好的问候,没有人
答案 4 :(得分:0)
以下代码完美无缺:
Private WithEvents txtmontant As DataGridViewTextBoxEditingControl
Private Sub DGdivers_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGdivers.EditingControlShowing
If DGdivers.CurrentCell.ColumnIndex = 1 Then
Dim txtmontant = CType(e.Control, DataGridViewTextBoxEditingControl)
AddHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
Else
RemoveHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
End If
End Sub
Private Sub txtmontant_keypress(sender As Object, e As KeyPressEventArgs) Handles txtmontant.KeyPress
If e.KeyChar = vbCr Then
DGdivers.Rows.Add()
Exit Sub
End If
If e.KeyChar = vbBack Then
Exit Sub
End If
If InStr("0123456789.,", e.KeyChar) = 0 Then
e.KeyChar = ""
End If
End Sub
答案 5 :(得分:0)
我使用KeyUp事件而不是KeyPress。技巧是在单元格处于活动状态时将处理程序事件附加到窗体的ActiveControl属性。
Private Sub grid_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles grid.CellBeginEdit
AddHandler Me.ActiveControl.KeyUp, AddressOf Cell_KeyUp
End Sub
Private Sub Cell_KeyUp(sender As Object, e As KeyEventArgs)
Console.WriteLine(sender.Text) 'content of cell
End Sub
如果还想使用RemoveHandler方法,则可以向该类添加一个全局变量(使用此变量代替Me.ActiveControl),然后将RemoveHandler例如调用到DataGridView的CellEndEdit事件中。