C# textBox’a Sadece Harf Girme, 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.

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir