Fendy Plick Fendy Plick - 7 months ago 40
Vb.net Question

keypress on datagridview only character "abcde" and convert to uppercase

How to limit

keypress
on
datagridview
only character abcde and convert to uppercase at vb.net?

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 3 Then
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBoxabcde_keyPress
End If
End Sub

Private Sub TextBoxabcde_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And e.KeyChar <> "." Then
e.Handled = True
End If
End Sub

Answer

Updated:

Private Sub DataGridView1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
    Dim allLetters As String = "abcde"
    If Not allLetters.Contains(e.KeyChar.ToString.ToLower) Then
        e.KeyChar = ChrW(0)
        e.Handled = True
    End If
End Sub

Private Sub dataGridView1_CellFormatting(sender As Object, e As 
DataGridViewCellFormattingEventArgs)
    If e.Value IsNot Nothing Then
        e.Value = e.Value.ToString().ToUpper()
        e.FormattingApplied = True
    End If
End Sub
Comments