Muitas vezes tratamos isso ao entrar ou sair do campo, mas pode-se também tratar enquanto se digita no campo, em tempo real.
Veja como se faz:
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox t = (TextBox)sender;
if (t.Text.Length > 1 && t.Text.Substring(t.Text.Length - 1, 1) == " ")
{
// Primeiras letras em maiúscula
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());
}
else if (t.Text.Length == 0 || t.SelectionLength == t.Text.Length)
{
// Primeira de todas em maiúscula
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());
}
else
// O resto minúsculo
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToLower());
}
Nenhum comentário:
Postar um comentário