本文介绍了vb.net在Custom控件上检测KeyDown,然后在Parent窗体上检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我创建了一个Custom TextBox,即TextBoxX,继承自TextBox类。我已经覆盖了KeyDown事件以添加一些默认快捷方式,这样我就可以在整个项目中访问它们,无论我在哪里使用TextBox。 TextBoxX代码: 公共 类 TextBoxX 继承 Windows.Forms.TextBox 公共 Sub 新() 使用 我 .Font = 新 System.Drawing.Font( Microsoft Sans Serif, 9 。 75 ,FontStyle。常规) 结束 使用 结束 Sub 受保护的 覆盖 Sub OnKeyDown( ByVal e 正如 System.Windows.Forms.KeyEventArgs) MyBase .OnKeyDown(e) 选择 案例 e.KeyData 案例 Keys.Enter SendKeys.Send( {TAB}) e.Handled = True 案例 Keys.Back 如果 Me .TabIndex> 0 Me .Text = 然后 SendKeys.Send ( + {TAB}) e.Handled = True ElseIf Me .TabIndex> 0 然后 SendKeys.Send( + {TAB}) e.Handled = True 结束 如果 案例 Keys.Escape ' < ==这是需要帮助的地方。 如果 我 .Text<> 然后 Me .Text = e.Handled = True 结束 如果 案例 其他 结束 选择 结束 Sub 结束 类 此外,我已将KeyDown事件添加到另一个自定义控件,即CreateCo,我在其上添加了许多TextBoxX实例。 KeyDown事件Cod e for CreateCo: 私人 Sub Controls_KeyDown( ByVal 发​​件人作为 对象, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase .KeyDown 选择 案例 e.KeyData 案例 Keys.Escape ' < ====这是问题 ' 代码在这里处置CreateCo 案例 否则 结束 选择 结束 Sub 此处的代码无法成功运行。它不会将Key hit首先发送到Parent Control'CreateCo'&然后到'TextBoxX',同时也是。我希望通过两次按键也可以发生这种情况。 最后,我想要做的是: 如果我的TextBoxX包含一些文本,'escape'键应该首先清除文本,如果我再次按'escape'键,那么键击应该传递给'CreateCo',然后,它将处理CreateCo。 如果我使用各种'If ... Else ..'条件合并CreateCo的KeyDown事件中的所有上述KeyDown代码, 我想要什么做到了。 (当然在这种情况下我将从TextBoxX中删除KeyDown事件) 合并代码成功运行: 私有 Sub Controls_KeyDown( ByVal 发​​件人作为 对象, ByVal e As System.Windows.Forms.KeyEventArgs)句柄 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 选择 案例 e.KeyData 案例 Keys.Enter SendKeys.Send( {TAB}) e.Handled = True 案例 Keys.Back 如果 我 .ActiveControl .TabIndex> 0 TypeOf 我 .ActiveControl System.Windows.Forms.TextBox 和 Me .ActiveControl.Text = 然后 SendKeys.Send( + {TAB }) e.Handled = True ElseIf Me .ActiveControl.TabIndex> 0 不 TypeOf Me .ActiveControl System.Windows.Forms.TextBox 然后 SendKeys.Send( + {TAB }) e.Handled = True 结束 如果 案例 Keys.Escape 如果( TypeOf sender TextBox 和 sender.Text<> )或 _ ( TypeOf sender MaskedTextBox 和发​​件人。文字<> )然后 我 .ActiveControl.Text = ElseIf TypeOf sender ComboBox 然后 如果 DirectCast (sender,ComboBox).SelectedIndex> 0 然后 DirectCast ( sender,ComboBox).SelectedIndex = 0 DirectCast (sender,ComboBox).DroppedDown = False 否则 DirectCast (sender,ComboBox).DroppedDown = False btnCancel_Click() 结束 如果 其他 btnCancel_Click() 结束 如果 案例 否则 结束 选择 结束 Sub 但我不喜欢我想合并代码的原因很简单,如果我在TextBoxX中编写一次KeyDown事件,我将能够在整个项目中使用它而无需重新输入。 (避免代码冗余)。我将编写所有其他表格或自定义控件的KeyDown事件。 请帮助我!!!! 先谢谢。解决方案 你的问题在这里: 受保护的 覆盖 Sub OnKeyDown( ByVal e As System.Windows.Forms.KeyEventArgs) 你实际上应该使用更像这样的东西, 私有 Sub MyBase_KeyDown( ByVal 发​​件人作为 对象, ByVal e 作为 System.Windows.Forms.KeyEventArgs)句柄 MyBase .KeyDown 您的代码问题在于您实际上并未处理任何类型的事件。当覆盖已经建立的事件时,您应该始终处理基本(MyBase)控件或当前控件(Me)本身。在上面的更正通知中: 句柄 MyBase .KeyDown 您的例程实际上并没有处理任何事情。 下面是我重新创建的代码,试图找到解决方案希望它有所帮助。 TextBoxX 公共 类 TextBoxX 继承 System.Windows.Forms.TextBox 公共 Sub New () 使用 Me .Font = 新 System.Drawing.Font( Microsoft Sans Serif, 9 。 75 ,FontStyle.Regular) 结束 结束 Sub 私有 Sub MyBase_KeyDown( ByVal 发​​件人作为 对象, ByVal e As System.Windows.Forms.KeyEventArgs)句柄 MyBase .KeyDown 选择 案例 e.KeyData 案例 Keys.Enter SendKeys.Send( {TAB}) e.Handled = True 案例 Keys.Back 如果 Me .TabIndex> 0 Me .Text = 然后 SendKeys.Send ( + {TAB}) e.Handled = True ElseIf Me .TabIndex> 0 然后 SendKeys.Send( + {TAB}) e.Handled = True 结束 如果 案例 Keys.Escape ' < ==这是需要帮助的地方。 如果 我 .Text<> 然后 Me .Text = e.Handled = True 结束 如果 案例 其他 结束 选择 结束 Sub 结束 类 Form1 公众 类 Form1 私有 Sub TextBoxX1_KeyDown( ByVal sender As System。对象, ByVal e As System.Windows.Forms。 KeyEventArgs)句柄 TextBoxX1.KeyDown Debug.WriteLine( TextBoxX1_KeyDown Called!, Form1) 结束 Sub 结束 类 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 '<==this is where is need help. If Me.Text <> "" Then Me.Text = "" e.Handled = True End If Case Else End Select End Sub End ClassFurther, 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 '<====Here is the problem 'Code goes here to dispose CreateCo Case Else End Select End SubCode 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 SubBut 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. 解决方案 Your issue is here:Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)You should actually be using something more like this,Private Sub MyBase_KeyDown(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDownThe 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:Handles MyBase.KeyDownYour routine does not actually handle anything. Below is the code I recreated to try and come to a solution hope it helps.TextBoxXPublic 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 SubEnd ClassForm1Public 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 SubEnd Class 这篇关于vb.net在Custom控件上检测KeyDown,然后在Parent窗体上检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 14:41