Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a Custom TextBox, viz., TextBoxX, inherited from the TextBox Class. I have overridden KeyDown event to add some default shortcuts, so that I can access them across my entire project, wherever I use my TextBox.

Code for TextBoxX:
VB
Public Class TextBoxX
        Inherits Windows.Forms.TextBox
        Public Sub New()
            With Me
                .Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75, FontStyle.Regular)
            End With
        End Sub
        Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            MyBase.OnKeyDown(e)
            Select Case e.KeyData
                Case Keys.Enter
                    SendKeys.Send("{TAB}")
                    e.Handled = True
                Case Keys.Back
                    If Me.TabIndex > 0 And Me.Text = "" Then
                        SendKeys.Send("+{TAB}")
                        e.Handled = True
                    ElseIf Me.TabIndex > 0 Then
                        SendKeys.Send("+{TAB}")
                        e.Handled = True
                    End If

                Case Keys.Escape           '<==this is where is need help.
                    If Me.Text <> "" Then
                        Me.Text = ""
                        e.Handled = True
                    End If
                Case Else
            End Select
        End Sub
    End Class


Further, I have added KeyDown event to another Custom Control, viz., CreateCo, on which I have added many instances of TextBoxX.

KeyDown event Code for CreateCo:
VB
Private Sub Controls_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyData
            Case Keys.Escape              '<====Here is the problem
                'Code goes here to dispose CreateCo
            Case Else
        End Select
    End Sub


Code upto here doesn't run successfully. It doesn't sends Key hit first to the Parent Control 'CreateCo' & then to the 'TextBoxX' and that too at the same time. I want otherewise to happen and that too with two key presses.

Finally, what I want to do here is:
If my TextBoxX contains some text, the 'escape' key should clear the text first and if I again press 'escape' key then the key hit should be passed on to 'CreateCo', which., in turn, will dispose CreateCo.

If I merge all the aforesaid KeyDown code in KeyDown event of CreateCo using various 'If... Else.. ' conditions,
what I wish to do is achieved. (of course in that case I will remove KeyDown event from TextBoxX)

The Merged code running successfully:
VB
Private Sub Controls_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown, txtPrintName.KeyDown, txtAdd.KeyDown, txtCity.KeyDown, txtPIN.KeyDown, _
        txtLline.KeyDown, txtEMail.KeyDown, txtRegNo.KeyDown, txtSales.KeyDown, txtCST.KeyDown, txtPT.KeyDown, txtPath.KeyDown, txtUID.KeyDown, txtUName.KeyDown, txtPwd.KeyDown, txtAns.KeyDown, _
        mtxtMobile.KeyDown, mtxtPAN.KeyDown, mtxtTAN.KeyDown, mtxtExcise.KeyDown, mtxtService.KeyDown, cmbState.KeyDown, cmbCountry.KeyDown, cmbStatus.KeyDown, cmbSecretQ.KeyDown, _
        dpickStartY.KeyDown, dpickStartB.KeyDown, chkSecurity.KeyDown
        Select Case e.KeyData
            Case Keys.Enter
                SendKeys.Send("{TAB}")
                e.Handled = True
            Case Keys.Back
                If Me.ActiveControl.TabIndex > 0 And TypeOf Me.ActiveControl Is System.Windows.Forms.TextBox And Me.ActiveControl.Text = "" Then
                    SendKeys.Send("+{TAB}")
                    e.Handled = True
                ElseIf Me.ActiveControl.TabIndex > 0 And Not TypeOf Me.ActiveControl Is System.Windows.Forms.TextBox Then
                    SendKeys.Send("+{TAB}")
                    e.Handled = True
                End If
            Case Keys.Escape
                If (TypeOf sender Is TextBox And sender.Text <> "") Or _
                    (TypeOf sender Is MaskedTextBox And sender.Text <> "") Then
                    Me.ActiveControl.Text = ""
                ElseIf TypeOf sender Is ComboBox Then
                    If DirectCast(sender, ComboBox).SelectedIndex > 0 Then
                        DirectCast(sender, ComboBox).SelectedIndex = 0
                        DirectCast(sender, ComboBox).DroppedDown = False
                    Else
                        DirectCast(sender, ComboBox).DroppedDown = False
                        btnCancel_Click()
                    End If
                Else
                    btnCancel_Click()
                End If
            Case Else
        End Select
    End Sub


But I don't want to merge the code for a simple reason that if I write KeyDown event once in TextBoxX, I will be able to use it across my entire project without retyping it. (To avoid redundancy in code). I will have write the KeyDown event of all my other forms or Custom Controls.

Please help me on this!!!!

Thanks in advance.
Posted

1 solution

Your issue is here:
VB.NET
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)


You should actually be using something more like this,

VB.NET
Private Sub MyBase_KeyDown(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown


The issue with your code is that you don't actually handle any sort of event. When "overriding" already established events you should always handle the base (MyBase) control or the current control (Me) itself. In my above correction notice:

VB.NET
Handles MyBase.KeyDown


Your routine does not actually handle anything.

Below is the code I recreated to try and come to a solution hope it helps.

TextBoxX
VB.NET
Public Class TextBoxX
    Inherits System.Windows.Forms.TextBox

    Public Sub New()
        With Me
            .Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75, FontStyle.Regular)
        End With
    End Sub

    Private Sub MyBase_KeyDown(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyData
            Case Keys.Enter
                SendKeys.Send("{TAB}")
                e.Handled = True
            Case Keys.Back
                If Me.TabIndex > 0 And Me.Text = "" Then
                    SendKeys.Send("+{TAB}")
                    e.Handled = True
                ElseIf Me.TabIndex > 0 Then
                    SendKeys.Send("+{TAB}")
                    e.Handled = True
                End If

            Case Keys.Escape           '<==this is where is need help.
                If Me.Text <> "" Then
                    Me.Text = ""
                    e.Handled = True
                End If
            Case Else
        End Select
    End Sub

End Class


Form1
VB.NET
Public Class Form1

    Private Sub TextBoxX1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxX1.KeyDown
        Debug.WriteLine("TextBoxX1_KeyDown Called!", "Form1")
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900