C# textBox’a Sadece Harf Girme veya Sadece Sayı Girme

C# textBox’a Sadece Harf Girme veya Sadece Sayı Girme

C#’ta textBox nesnesine sadece harf veya sadece rakam girişi yapmak için textBox’ın KeyPress eventine tek satırdan oluşan şu kodu yazmanız yeterlidir:
  • textBox’a sadece harf girişi için:
1
2
3
4
5
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     e.Handled = !char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)
                 && !char.IsSeparator(e.KeyChar);
}
  • textBox’a sadece rakam girişi için:
1
2
3
4
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
     e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
satırlarını yazmanız yeterlidir.

Yorumlar

Bu blogdaki popüler yayınlar

Asp.Net ile T.C. Kimlik Doğrulama (Mernis) Web Service ile

Chrome HSTS Hatası Nedir ve Nasıl Çözülür?

Btk Akademi Sertifika Meb Onaylı mı Veya Geçerli mi?