简体   繁体   中英

DatagridView Event To allow Numbers, Backspace & Delete Keys Only VB.Net

How to alter the below code to accept Delete and BackSpace Keys also??

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

Current Code After Modification - Not Working

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

EDIT - 2 Code

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

Now getting error in TextBox_keyDown Word this line...

AddressOf TextBox_keyDown

Error Text

Severity Code Description Project File Line Suppression State Error BC31143 Method 'Private Sub TextBox_keyDown(sender As Object, e As KeyEventArgs)' does not have a signature compatible with delegate 'Delegate Sub KeyPressEventHandler(sender As Object, e As KeyPressEventArgs)'.

Change the DataGridview EditMode Property to EditOnEnter and use the below code.

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

This will allow only numbers, backspace, arrow keys, delete, home, end, spacebar in column-1 of the datagridview.

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;
                }
            }
        }       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM