<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>限制控件可接受的键击</Title>
      <Author>Microsoft Corporation</Author>
      <Description>重写 Windows 窗体控件的默认 ProcessCmdKey 函数，并将用户输入限制为数字键和导航键。</Description>
      <Shortcut>formRestrict</Shortcut>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.Drawing.dll</Assembly>
        </Reference>
        <Reference>
          <Assembly>System.Windows.Forms.dll</Assembly>
        </Reference>
        <Reference>
          <Assembly>System.dll</Assembly>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>Microsoft.VisualBasic</Namespace>
        </Import>
        <Import>
          <Namespace>System.Drawing</Namespace>
        </Import>
        <Import>
          <Namespace>System.Windows.Forms</Namespace>
        </Import>
        <Import>
          <Namespace>System</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>baseClass</ID>
          <Type>String</Type>
          <ToolTip>替换为要限制的类的类型。</ToolTip>
          <Default>ComboBox</Default>
        </Literal>
        <Literal>
          <ID>restrictedControlClass</ID>
          <Type>String</Type>
          <ToolTip>派生类的名称。</ToolTip>
          <Default>restrictedComboBoxClass</Default>
        </Literal>
      </Declarations>
      <Code Language="VB" Kind="type decl"><![CDATA[Class $restrictedControlClass$
    Inherits $baseClass$
    Const WM_KEYDOWN As Integer = &H100

    Protected Overrides Function ProcessCmdKey _
        (ByRef msg As Message, _
        ByVal keyData As Keys) As Boolean

        If msg.Msg = WM_KEYDOWN Then
            Return Not ((keyData >= Keys.D0 And keyData <= Keys.D9) _
                Or keyData = Keys.Back Or keyData = Keys.Left _
                Or keyData = Keys.Right Or keyData = Keys.Up _
                Or keyData = Keys.Down Or keyData = Keys.Delete
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
End Class
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
