.NET Framework クラス ライブラリ
KeyPressEventArgs.KeyChar プロパティ

押されたキーに対応する文字を取得または設定します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

構文

Visual Basic (宣言)
Public Property KeyChar As Char
Visual Basic (使用法)
Dim instance As KeyPressEventArgs
Dim value As Char

value = instance.KeyChar

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

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

public function set KeyChar (value : char)

プロパティ値

作成される ASCII 文字。たとえば、ユーザーが Shift キーを押しながら K キーを押した場合、このプロパティは大文字の K を返します。
解説

KeyChar プロパティを使用すると、実行時にキーストロークをサンプリングしたり、特殊な実行環境下でキーストロークを変更したりできます。たとえば、KeyChar を使用して、ユーザーが郵便番号を入力するときに数字以外のキー入力を無効にしたり、データ入力フィールドでアルファベットのキー入力をすべて大文字に変更したりできます。また、キーボードやその他のキー入力デバイスで、特定のキーの組み合わせを監視することもできます。

次のキーを取得または設定できます。

  • a ~ z、A ~ Z の各キー

  • Ctrl キー

  • 区切り記号

  • 数字キー (キーボード上段のキーとテンキーの両方)

  • Enter キー

次のキーは取得または設定できません。

  • Tab キー

  • Ins キーと Del キー

  • Home キー

  • End キー

  • PageUp キーと PageDown キー

  • F1 ~ F2 の各キー

  • Alt キー

  • 方向キー

使用例

TextBox コントロールを作成する例を次に示します。keypressed メソッドは、KeyChar プロパティを使用して Enter キーが押されたかどうかを確認します。Enter キーが押された場合、Handled プロパティは true に設定され、イベントが処理されることを示します。

Visual Basic
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
C#
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());
    }
}
C++
#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 );
}
J#
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
参照

タグ :


Page view tracker