簡體   English   中英

DatagridView 事件僅允許數字、退格和刪除鍵 VB.Net

[英]DatagridView Event To allow Numbers, Backspace & Delete Keys Only 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 Word 中出現錯誤這一行...

AddressOf TextBox_keyDown

錯誤文本

嚴重性代碼描述項目文件行抑制狀態錯誤 BC31143 方法“Private Sub TextBox_keyDown(sender As Object,e As KeyEventArgs)”沒有與委托“Delegate Sub KeyPressEventHandler(sender As Object,e As KeyPressEventArgs)”兼容的簽名。

將 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 列中的數字、退格鍵、箭頭鍵、刪除、主頁、結尾、空格鍵。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM