Search code examples
c#-4.0textboxvalidating-event

how to validate the Textbox allows only first character Capital latter in c#.net?


I have a task, my textbox accepts only first character capital lettr and remaing characters normal.


Solution

  • Use KeyPress event of textbox:

    
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txt.Text != "" && !Char.IsUpper(txt.Text, 0))
            {
                txt.Text = Char.ToUpper(txt.Text[0]) + txt.Text.Substring(1);
            }
        }