我想知道是否有任何方法可以将自定义常量添加到系统常量中。
例如,在我的编码中,我经常使用这样的代码:
VB.NET
Private Sub myform_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F3 Or (e.Control And e.KeyCode = Keys.F) Then
search(mytextbox.Text.Trim)
End If
End Sub
C#
private void myform_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F3 | (e.Control & e.KeyCode == Keys.F)) {
search(mytextbox.Text.Trim);
}
}
想法是向Enum Keys添加新元素,说“查找” 所以我能够做到这一点:
VB.NET
If e.KeyCode = Keys.Find Then
search(mytextbox.Text.Trim)
End If
C#
if (e.KeyCode == Keys.Find) {
search(mytextbox.Text.Trim);
}
当然,我首先将Keys.F3和e.Control + Keys.F的键击更换为Keys.Find,在Applicaton级别的某个较低级别“ProcessCmdKey”中,如下所示:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.F3 Then
msg.WParam = CType(Keys.Find, IntPtr)
keyData = Keys.Find
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
所以,问题是: Keys.Find可以添加到Keys常量中,如果可以 - 如何?
答案 0 :(得分:0)
不,因为您无法控制数据来源(e.KeyCode
)。假设您可以在枚举中添加一个新值,当它检测到控件和键的组合时,您如何告诉Windows开始向您发送此特定新值?