vb.net 捕获 Datagridview 单元格 kepress 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14978620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:25:37  来源:igfitidea点击:

Capture Datagridview cell kepress event

vb.net

提问by Furqan Sehgal

Could any body kindly give me code example how to capture datagridview cell keypress event? Datagridview_keypress does not help.

任何人都可以给我代码示例如何捕获 datagridview 单元格按键事件吗?Datagridview_keypress 没有帮助。

Thanks

谢谢

回答by bmadtiger

As per Fco Navarro's answer, except that using e.Controldoes not always work because eis passed in to the EditingControlShowingevent ByValmeaning any changes to the control (eg changing the .Textproperty) are NOT reflected in the DataGridView. If you need to do anything with the actual TextBox control in your event handler, you can use DataGridView1.EditingControlinstead of e.Control.

根据 Fco Navarro 的回答,除了 usinge.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

回答by Fco Navarro

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

回答by SysDragon

Try this:

尝试这个:

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

Extracted from here.

这里提取。

回答by noonecares

if your using your gridview in asp.net (Website) its not possible. there is no keypress Event. its possible indeed but you have to use JavaScript to make a postback on every keychanged (Client side). but this is not a nice programming style and should not be used (you are getting rly many postbacks which are slowing down the complete System).

如果您在 asp.net(网站)中使用 gridview,则不可能。没有按键事件。它确实可能,但您必须使用 JavaScript 在每个键更改(客户端)上进行回发。但这不是一个很好的编程风格,不应该使用(你会得到很多会减慢整个系统速度的回发)。

if your using Windows forms: have a look at the answer from sysdragon.

如果您使用 Windows 表单:看看来自 sysdragon 的答案。

hope this helps a bit.

希望这个对你有帮助。

best regards, noone

最好的问候,没有

回答by Naoufel Bouslama

the following code work perfectly:

以下代码完美运行:

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