0
如何将下面的代码改为接受删除和退格键也是??DatagridView事件允许仅限数字,退格键和删除键VB.Net
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Select Case DataGridView1.CurrentCell.ColumnIndex
Case Is = 0, 1
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
End Select
End Sub
Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then
e.Handled = True
End If
End Sub
目前代码修改后 - 不工作
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Select Case DataGridView1.CurrentCell.ColumnIndex
Case Is = 0, 1
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyDown
End Select
End Sub
Private Sub TextBox_keyDown(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then
e.Handled = True
End If
End Sub
编辑 - 2码
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Select Case DataGridView1.CurrentCell.ColumnIndex
Case Is = 0, 1
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyDown
End Select
End Sub
Private Sub TextBox_keyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If Not (Char.IsDigit(CChar(CStr(e.KeyValue))) Or e.KeyValue = ".") Then
e.Handled = True
End If
End Sub
在TextBox_keyDown字这条线现在越来越错误...
AddressOf TextBox_keyDown
错误文本
严重性代码说明项目文件的线路抑制状态 错误BC31143法“私人小组TextBox_keyDown(发送者为对象,E 作为KeyEventArgs)”不具有与委托 兼容的签名'Delegate Sub KeyPressEventHandler(sender As Object,e As KeyPressEventArgs)'。
你不能用'KeyPress'事件来做到这一点。你需要'KeyDown'事件。 –
我在两个地方都将它改为KeyDown,但它不允许我删除dgv数据。我错过了什么? – Sixthsense
发布您的当前代码并解释“不允许”的含义。 –