Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to check whether Textbox empty and if it is empty should display error using Textbox KeyPress Event and shouldn't be able to move to next control
Posted

1 solution

C#
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
 {
 if(e.KeyCode == Keys.Enter)
   {
      if(String.IsNullOrEmpty(textBox1.Text))
         {
            MessageBox("Textbox cannot be empty","Title", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textBox1.Focus();
         }
    else
        {
             //do something
        }  
   }                      
 }
}
 
Share this answer
 
v4
Comments
victowork 20-Aug-14 8:23am    
@Hard_Rockz here i can move the focus to another control how i can prevent that..........
Dilan Shaminda 20-Aug-14 11:37am    
here i have move the focus to the same textbox (textBox1.Focus()).Not to another control. If you want user to fill this one and move forward,you can disabled other controls until user fill this texbox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900