DatagridView事件允许数字,退格和&仅删除键VB.Net

时间:2016-08-01 08:01:12

标签: .net vb.net winforms datagridview vb.net-2010

如何更改以下代码以接受删除 BackSpace 键?

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中收到错误Word这一行......

AddressOf TextBox_keyDown

错误文字

  

严重级代码描述项目文件行抑制状态   错误BC31143方法'私有子TextBox_keyDown(发件人作为对象,e   如KeyEventArgs)'没有与代表兼容的签名   '委托子KeyPressEventHandler(发件人为对象,e As   KeyPressEventArgs)'

2 个答案:

答案 0 :(得分:0)

将DataGridview EditMode 属性更改为 EditOnEnter ,然后使用以下代码。

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 e.KeyChar <> ControlChars.Back Then
        e.Handled = Not (Char.IsDigit(e.KeyChar))
    End If
End Sub

答案 1 :(得分:0)

这将在datagridview的第1列中仅允许数字,退格键,箭头键,删除键,起始键,结束键,空格键。

private void DGV_Test_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
            if (DGV_Test.CurrentCell.ColumnIndex == 0) //Desired Column
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);                    
                }
            }
        }

        private void Column1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
            {
                if (e.KeyChar == 0x20)  // Allow space also
                {

                }
                else
                {
                    e.Handled = true;
                }
            }
        }