KeyPressEventArgs.KeyChar Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur karakter yang sesuai dengan tombol yang ditekan.
public:
property char KeyChar { char get(); };
public:
property char KeyChar { char get(); void set(char value); };
public char KeyChar { get; }
public char KeyChar { get; set; }
member this.KeyChar : char
member this.KeyChar : char with get, set
Public ReadOnly Property KeyChar As Char
Public Property KeyChar As Char
Nilai Properti
Karakter ASCII yang terdiri. Misalnya, jika pengguna menekan SHIFT + K, properti ini mengembalikan K huruf besar.
Contoh
Contoh berikut membuat TextBox kontrol. Metode keypressed ini menggunakan KeyChar properti untuk memeriksa apakah tombol ENTER ditekan. Jika tombol ENTER ditekan, Handled properti diatur ke true, yang menunjukkan peristiwa ditangani.
#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 );
}
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());
}
}
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
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
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
Keterangan
KeyChar Gunakan properti untuk sampel penekanan tombol pada waktu proses dan untuk memodifikasi penekanan tombol dalam keadaan run-time khusus. Misalnya, Anda dapat menggunakan KeyChar untuk menonaktifkan penekanan tombol non-numerik saat pengguna memasukkan kode pos, mengubah semua penekanan tombol alfabet menjadi huruf besar di bidang entri data, atau memantau keyboard atau perangkat input kunci lainnya untuk kombinasi kunci tertentu.
Anda bisa mendapatkan atau mengatur kunci berikut:
a-z, A-Z.
CTRL.
Tanda baca.
Tombol angka, baik di bagian atas keyboard maupun di keypad numerik.
MASUKKAN.
Anda tidak bisa mendapatkan atau mengatur kunci berikut:
Tombol TAB.
SISIPKAN dan HAPUS.
RUMAH.
AKHIR.
PAGE UP dan PAGE DOWN.
F1-F2.
ALT.
Tombol panah.
Nota
Untuk informasi tentang cara mendeteksi salah satu kunci non-karakter yang disebutkan di atas, lihat KeyEventArgs kelas .