[英]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编辑 - 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
Now getting error in TextBox_keyDown Word this line...现在在 TextBox_keyDown Word 中出现错误这一行...
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)'.严重性代码描述项目文件行抑制状态错误 BC31143 方法“Private Sub TextBox_keyDown(sender As Object,e As KeyEventArgs)”没有与委托“Delegate Sub KeyPressEventHandler(sender As Object,e As KeyPressEventArgs)”兼容的签名。
Change the DataGridview EditMode Property to EditOnEnter and use the below code.将 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
This will allow only numbers, backspace, arrow keys, delete, home, end, spacebar in column-1 of the 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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.