许多控件的 Keydown 处理程序 [英] Keydown Handler for Many Controls
本文介绍了许多控件的 Keydown 处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更好的方法为繁忙表单上的许多控件设置一个 keydown 处理程序?(在某些情况下,单独使用表单处理程序不起作用.)
Is there a better way to have a keydown handler for many controls on a busy form? (Using the form handler alone doesn't work in some cases.)
Private Sub rText_KeyDown(ByVal Sender As Object, ByVal e As KeyEventArgs) Handles rText0.KeyDown, txTaxon.KeyDown, txCommon.KeyDown, _
txConfidence.KeyDown, txDate.KeyDown, txDateAdded.KeyDown, txFileName.KeyDown, txGPS.KeyDown, txRating.KeyDown, _
txConfidence.KeyDown, txQuality.KeyDown, txRemarks.KeyDown, txKeyWords.KeyDown, txOriginalPath.KeyDown, txDateAdded.KeyDown, _
txLink.KeyDown, chkLink.KeyDown, rview.KeyDown, cmdNext.KeyDown, tvTaxon.KeyDown, Me.KeyDown, _
cmdTaxon.KeyDown
Call globalkey(e)
End Sub
推荐答案
我是一名 C# 程序员,因此无法提供可行的解决方案(如在 vb.net 代码中).
I am a C# programmer, and hence wont be able to provide a working solutions (as in vb.net code).
我认为您可以通过执行以下操作来实现这一点.
I think that you can get this working by doing following things.
- 编写一个AddHandlers 用于您各自的控件,当且仅当它们具有设置为属性的特定值(详细信息在下面的第 2 步中)
- 您希望将公共处理程序附加到的所有控件,设置它们的Tag 属性为唯一值,例如:
"KeyDown"
.在这种情况下,我建议使用Enum
而不是string
- 现在,在您的
Form
构造函数中,在InitializeComponent
调用之后,调用您在上面 #1 中编写的方法.这会将处理程序附加到所有所需的控件. - 如果您有多个具有此要求的表单,您可以将此功能添加到主表单中,该功能就像所有其他表单的父表单(继承)
- Write up a method which AddHandlers for your respective controls, if and only if, they have specific value set to a property (details in step #2 below)
- All controls, to which you want a common handler to be attached, set their Tag property to a unique value, for example :
"KeyDown"
. I would recommend use ofEnum
though in this case, instead ofstring
- Now, in your
Form
constructor, afterInitializeComponent
call, call the method you wrote in #1 above. This will attach handlers to all desired controls. - If you have multiple forms having this requirement, you can add this functionality to a main form, which acts like a parent for all others (inheritance)
希望我足够清楚,这会有所帮助.
Hope I am clear enough and this helps.
这篇关于许多控件的 Keydown 处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文