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:
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 sayı girişi için:
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
yazmanız yeterlidir.