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:
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
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:
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
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:
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.