KeyPressEventArgs.KeyChar 属性

获取或设置与按下的键对应的字符。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property KeyChar As Char
用法
Dim instance As KeyPressEventArgs
Dim value As Char

value = instance.KeyChar

instance.KeyChar = value
public char KeyChar { get; set; }
public:
property wchar_t KeyChar {
    wchar_t get ();
    void set (wchar_t value);
}
/** @property */
public char get_KeyChar ()

/** @property */
public void set_KeyChar (char value)
public function get KeyChar () : char

public function set KeyChar (value : char)

属性值

撰写的 ASCII 字符。例如,如果用户按下 Shift + K,则该属性返回一个大写的 K。

备注

使用 KeyChar 属性在运行时对击键取样,在特定运行时环境下修改击键。例如,您可以使用 KeyChar 在用户输入邮政编码时禁用非数字按键,将数据输入字段中所有的字母按键都改为大写,或监视键盘或其他按键输入设备的特定组合键。

您可以获取或设置以下键:

  • a-z,A-Z。

  • Ctrl。

  • 标点符号。

  • 键盘顶部和数字键盘上的数字键。

  • Enter。

您不能获取或设置以下键:

  • Tab 键。

  • Insert 和 Delete。

  • Home。

  • End。

  • Page Up 和 Page Down。

  • F1-F2。

  • Alt。

  • 箭头键。

示例

下面的示例创建一个 TextBox 控件。keypressed 方法使用 KeyChar 属性检查是否按了 Enter 键。如果按了 Enter 键,则 Handled 属性设置为 true,表示此事件已处理。

Imports System
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()
        ' Create a TextBox control.
        Dim tb As New TextBox()
        Me.Controls.Add(tb)
        AddHandler tb.KeyPress, AddressOf keypressed
    End Sub 'New

    Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
        ' The keypressed method uses the KeyChar property to check 
        ' whether the ENTER key is pressed. 

        ' If the ENTER key is pressed, the Handled property is set to true, 
        ' to indicate the event is handled.

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
        End If
    End Sub 'keypressed

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub 'Main
End Class 'Form1
using System;
using System.Windows.Forms;

public class Form1: Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.Controls.Add(tb);
        tb.KeyPress += new KeyPressEventHandler(keypressed);
    }

    private void keypressed(Object o, KeyPressEventArgs e)
    {
        // The keypressed method uses the KeyChar property to check 
        // whether the ENTER key is pressed. 

        // If the ENTER key is pressed, the Handled property is set to true, 
        // to indicate the event is handled.
        if (e.KeyChar == (char)Keys.Return)
        {
            e.Handled = true;
        }
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

public ref class Form1: public Form
{
public:
   Form1()
   {
      // Create a TextBox control.
      TextBox^ tb = gcnew TextBox;
      this->Controls->Add( tb );
      tb->KeyPress += gcnew KeyPressEventHandler( this, &Form1::keypressed );
   }

private:
   void keypressed( Object^ /*o*/, KeyPressEventArgs^ e )
   {
      // The keypressed method uses the KeyChar property to check 
      // whether the ENTER key is pressed. 
      // If the ENTER key is pressed, the Handled property is set to true, 
      // to indicate the event is handled.
      if ( e->KeyChar == (char)13 )
            e->Handled = true;
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}
import System.*;
import System.Windows.Forms.*;

public class Form1 extends Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.get_Controls().Add(tb);
        tb.add_KeyPress(new KeyPressEventHandler(KeyPressed));
    } //Form1

    void KeyPressed(Object o, KeyPressEventArgs e)
    {
        // The keypressed method uses the KeyChar property to check 
        // whether the ENTER key is pressed. 
        // If the ENTER key is pressed, the Handled property is set to true, 
        // to indicate the event is handled.
        if (e.get_KeyChar() == (char)(13)) {
            e.set_Handled(true);
        }
    } // KeyPressed

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main
} //Form1

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

KeyPressEventArgs 类
KeyPressEventArgs 成员
System.Windows.Forms 命名空间
Control.KeyPress 事件
IsInputChar