validating textbox in windows form applications

前端 未结 7 1757
北荒
北荒 2020-12-12 08:21

My scenario is:

Not allowing spaces at starting position of textbox after enter one or more characters text box allows spaces

Below not applicable to my sce

7条回答
  •  青春惊慌失措
    2020-12-12 08:28

    Try

     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)         
     {             
         if (e.Handled = (e.KeyChar == (char)Keys.Space))             
         {                 
            if(((TextBox)sender).Text.Replace(" ","") == "")
            {
                 MessageBox.Show("Spaces are not allowed");  
                 ((TextBox)sender).Text = string.Empty;
            }           
         }          
     } 
    

提交回复
热议问题