Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 scenario.

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.Handled = (e.KeyChar == (char)Keys.Space))
        {
            MessageBox.Show("Spaces are not allowed");
        }
    }
    
  2. textBox1.Text.TrimStart()
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
273 views
Welcome To Ask or Share your Answers For Others

1 Answer

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
if(textBox1.Text.Length == 0)
    {
    if (e.Handled = (e.KeyChar == (char)Keys.Space)) 
        { 
        MessageBox.Show("Spaces are not allowed at start"); 
        } 
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...