none
Tapping Delete Key in VB.NET !?!? RRS feed

  • Question

  • Hi friends,

    I'm creating an application for which I need to tap "Delete" key. I need to trap that key in "KeyPress" event, but am not able to do that directly. "Delete" key can be tapped in "KeyUp" or "KeyDown" event, but in that case, it'll not register the count for which the key has been pressed.

    Please help me out for that.

    Solutions are invited for the same.
    Friday, September 7, 2007 3:41 PM

Answers

  • I've tried one of the code, that's as follows. But am looking forward for more efficient version. If somebody can make out, it'll be of great help.




        Private Sub txtCodeShare_Handler(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles txtCodeShare.KeyDown

            'This is special case as if "Delete" key is pressed,
            'it cannot be trapped in KeyPress event.
            If e.Control = True Then
                Exit Sub
            End If

            If e.KeyCode = Keys.Delete Then

                If txtCodeShare.SelectionLength <> 0 Then
                    Exit Sub
                End If

                'Formulate the message.
                Dim Message As String = CODE_SHARE & "["

                'Check who to chat with.
                Dim user As Object
                For Each user In lstUsers.SelectedItems
                    Message += user & ","
                Next
                Message += "]" & txtNick.Text & " > " & Keys.Delete & _
                        "," & txtCodeShare.SelectionStart.ToString & _
                        "," & intDeleteCharactersCount.ToString

                'Send Message.
                fncSendMessage(Message)

                'Resetting this value is needed as it might cause
                'troubles if "Delete" key is pressed multiple times
                'in empty TextBox.
                intDeleteCharactersCount = 0
            Else
                'Storing selected characters count temporarily as
                'pressing "Delete" key will force to loose this
                'count.
                intDeleteCharactersCount = txtCodeShare.SelectionLength

            End If
        End Sub
    Friday, September 7, 2007 3:47 PM

All replies

  • I've tried one of the code, that's as follows. But am looking forward for more efficient version. If somebody can make out, it'll be of great help.




        Private Sub txtCodeShare_Handler(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles txtCodeShare.KeyDown

            'This is special case as if "Delete" key is pressed,
            'it cannot be trapped in KeyPress event.
            If e.Control = True Then
                Exit Sub
            End If

            If e.KeyCode = Keys.Delete Then

                If txtCodeShare.SelectionLength <> 0 Then
                    Exit Sub
                End If

                'Formulate the message.
                Dim Message As String = CODE_SHARE & "["

                'Check who to chat with.
                Dim user As Object
                For Each user In lstUsers.SelectedItems
                    Message += user & ","
                Next
                Message += "]" & txtNick.Text & " > " & Keys.Delete & _
                        "," & txtCodeShare.SelectionStart.ToString & _
                        "," & intDeleteCharactersCount.ToString

                'Send Message.
                fncSendMessage(Message)

                'Resetting this value is needed as it might cause
                'troubles if "Delete" key is pressed multiple times
                'in empty TextBox.
                intDeleteCharactersCount = 0
            Else
                'Storing selected characters count temporarily as
                'pressing "Delete" key will force to loose this
                'count.
                intDeleteCharactersCount = txtCodeShare.SelectionLength

            End If
        End Sub
    Friday, September 7, 2007 3:47 PM
  •  

    Sanket,

    Unlike backspace which has a character \b, there is no character for delete. So technically you cant trap it in the keydown event.

     

    If you want, you can override the keydown event and enable keypreview for your form.

     

    Code Snippet
    protected override void OnKeyDown(KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Delete)
    e.Handled = true;
    }

     

    You could also try overriding PreProcessMessage as it might do the trick.

     

    Good luck!

    Saturday, September 8, 2007 4:56 AM
  • Hey Adnan,

    Thanks for the help. But I want code in VB.NET, and not is C#.

    If possible, please send the code for VB.NET.

    Anyways, thanks for your efforts. Smile
    Saturday, September 8, 2007 1:39 PM
  • Only 5 years late, but here's my VB.NET code I used to capture the delete key.

    The control is a datagrid and when the delete key is caught, I set the contents of all selected cells in the datagrid to ""

    Heres the code:

        Private Sub datagridDilution_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles datagridDilution.KeyDown
            If e.KeyCode = Keys.Delete Then
                Dim byteSelected_Cells_Count As Byte
                For byteSelected_Cells_Count = 0 To datagridDilution.SelectedCells.Count - 1
                    datagridDilution.SelectedCells.Item(byteSelected_Cells_Count).Value = ""
                Next
            End If
        End Sub


    Kristian Benning Sharepoint Newbie (Slightly better at VBA and SQL databases)

    Tuesday, December 4, 2012 11:43 AM